Tutorial Downloads .com

Home

BEGINNING PHP4 : GENERATING GRAPHICS PART 3 - PUTTING IT ALL TOGETHER

BEGINNING PHP4 : GENERATING GRAPHICS PART 3 - PUTTING IT ALL TOGETHER DESCRIPTION So far we've covered creating an image, outputting it to the browser and cleaning up after ourselves. Between creating the image and outputting it we also covered creating lines, circles and rectangles on our image.

This free tutorial is a sample from the book Beginning Php 4 (Programmer to Programmer)


PHP currently has no function that allows us to create a rectangle with rounded corners. In order to recap what we covered so far, we're going to create a function that does just that. We'll be able to pass this function the same information that you would pass to ImageRectangle(), but with an extra argument telling it the radius of the arc that we want to use for the corner. Our prototype will therefore be:

udImageRoundRect($image, $x1, $y1, $x2, $y2, $arcradius, $color)

We'll never use $x1, $y1, $x2, or $y2 as actual points in any of the plots, but we will use them to work out where our arcs must be centered, as well as where our lines must start and end. In the image above, we know that ($x1, $y1) is where our rectangle would have started if it didn't have a rounded corner. We can work out where the center of the arc must be by adding $arcradius to each of $x1 and $y1. Remember that we will only be adding to both x and y values in the top left-hand corner of the rectangle. In other corners we will have to subtract $arcradius from one or both of the values.

Let's dive into the code and take a look. We're going to define our function in an include file called roundrect.inc:

//roundrect.inc
function udImageRoundRect($image,$x1,$y1,$x2,$y2,$arcradius,$color) {

$arcwidth = ($arcradius*2);

// top left hand corner
ImageArc($image, $x1+$arcradius, $y1+$arcradius,
$arcwidth, $arcwidth,
180, 270,
$color);

The first thing we do is to double $arcradius, giving us the width of the arc; we can pass this directly to ImageArc().

The next line draws the top left-hand corner arc. The center of the arc is at:

  • x = $x1 + $arcradius (just to the right of the corner of the rectangle),
  • y = $y1 + $arcradius (just down from the same corner).

The width and height of the arc are both equal to $arcwidth, since we want the corner to be rounded, not ellipsoid. We start the arc at 180° (9 o'clock) and end at 270° (12 o'clock) - a 90° arc. As we move around the corners our degrees will shift by 90° each time.

// top right hand corner
ImageArc($image, $x2-$arcradius, $y1+$arcradius,
$arcwidth, $arcwidth,
270, 360, $color);

Now that we're at the top right-hand corner we must use the $x2 value and this time subtract $arcradius - the center of the arc will be to the left of the rectangle corner. We are still working with $y1 and since it's at the same horizontal level, we still use $y1 + $arcradius. As you can see in the diagram opposite, we also need to shift each of our degrees clockwise by 90 degrees.

Each of the bottom corners work in exactly the same way; you just have to remember whether to add or subtract, and which x and y values you should be working with:

// bottom right hand corner
ImageArc($image, $x2-$arcradius, $y2-$arcradius,
$arcwidth, $arcwidth,
0, 90, $color);

// bottom left hand corner
ImageArc($image, $x1+$arcradius, $y2-$arcradius,
$arcwidth, $arcwidth,
90, 180, $color);

The last part of our script draws in the connecting lines between the rounded corners. Now if we were going to draw in our top line as if we weren't using rounded corners, we would simply use ($x1, $y1) as our first coordinate and ($x2, $y1) as our second coordinate. Since we have to take the corners into account, we must adjust some of these coordinates by $arcradius:

// top line
ImageLine($image, $x1+$arcradius, $y1,
$x2-$arcradius, $y1, $color);
// right line
ImageLine($image, $x2, $y1+$arcradius,
$x2, $y2-$arcradius, $color);

// bottom line
ImageLine($image, $x1+$arcradius, $y2,
$x2-$arcradius, $y2, $color);
// left line
ImageLine($image, $x1, $y1+$arcradius,
$x1, $y2-$arcradius, $color);
}
?>

And that's it for roundrect.inc. We can now write a PHP script like this:

//roundrect.php
Header("Content-type: image/jpeg");
include "roundrect.inc";
$image = ImageCreate(200,150);
$gray = ImageColorAllocate($image,204,204,204);
$blue = ImageColorAllocate($image,0,0,255);
udImageRoundRect($image,10,10,190,140,30,$blue);
ImageJPEG($image);
ImageDestroy($image);
?>

The first line includes the roundrect.inc file created above, so we have access to our new user-defined function udImageRoundRect():

udImageRoundRect($image,10,10,190,140,30,$blue);

We start the rectangle at (10,10) and end it at (190,140); 10 pixels clear of each edge of the image. The radius of our corner arcs will be 30 pixels. If we run the script we get an image like this:


Resource:
Posted By : PHPbrain
On date : 06.03.08

Most used PHP Tutorials

PHP Tutorials ( Ebooks , PDF's )
A Programmer’s Introduction to PHP 4.0 John.Wiley.and.Sons.PHP5.and.MySQL.Bible.eBook.pdf New Riders - PHP Functions Essential Reference.chm The Underground PHP and Oracle Manual
BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION
BEGINNING PHP4 : GENERATING GRAPHICS PART 1 - LAYING A FOUNDATION DESCRIPTION PHP contains a range of functions that allow you to open, manipulate and output graphics to the web browser. During this chapter we will explore how these functions work and how we can apply them to display our data.
PHP Handout ( php pdf study material )
ATDB - 2004 PHP Handout - Volume 3 - PHP tutorial. Page - 1. Vol. 3 : PHP tutorial. PHP Handout. ATDB 2004. Page 2. ATDB - 2004 PHP Handout - Volume 3 - PHP …
PHP/MySQL Tutorial
Open source has brought a lot more than Linux to the computing world. It has also given us PHP and MySQL. According to Graeme, PHP and MySQL are the world’s best combination for creating data-driven sites. In the first installment of this three-lesson tutorial, our Kiwi guide covers everything you need to know to begin developing database hubs. He gives instructions for installation on both Unix and Windows, and then goes on to show some simple scripts that will insert information into a database and display that data on a Web page.
Beginning PHP and MySQL: From Novice to Professional, Third Edition
A comprehensive introduction to … the scripting language PHP and the MySQL database server. This book will not only expose … core aspects of both technologies, but will provide valuable insight into how they are used in unison to create dynamic data-driven Web applications.
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites
If you know HTML, this guide will have you building interactive websites quickly. You'll learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript, regardless of whether you already know how to program. Discover how the powerful combination of PHP and MySQL provides an easy way to build modern websites complete with dynamic data and user interaction. You'll also learn how to add JavaScript to create rich Internet applications and websites.
Magento Beginner's Guide
Get your Magento store up and running using Magento Beginner's Guide by William Rice.
Database Handling with PHP/MySQL Tutorial ( php pdf study material )
This php pdf study material gives Database Handling with PHP/MySQL Tutorial.Unlike other scripting languages for Web page development (i.e. ASP), PHP is open-source, crossplatform, and offers excellent connectivity to most of today’s common databases including Oracle, Sybase, Microsoft SQL Server, MySQL, Postgresql, ODBC (and others). PHP also offers integration with various external libraries which enable the developer to do anything from generating PDF docum ents, accessing secure payment services and producing graphic output, to parsing XML.
PHP and MySQL Web Development - tutorial Book
The PHP server-side scripting language and the MySQL database management system (DBMS) make a potent pair. Both are open-source products--free of charge for most purposes--remarkably strong, and capable of handling all but the most enormous transaction loads. Both are supported by large, skilled, and enthusiastic communities of architects, programmers, and designers. PHP and MySQL Web Development introduces readers (who are assumed to have little or no experience with the title subjects) to PHP and MySQL for the purpose of creating dynamic Internet sites. It teaches the same skills as introductory Active Server Pages (ASP) and ColdFusion books--technologies that address the same niche.
PHP - Advanced Tutorial
PHP - Advanced Tutorial
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
It hasn't taken Web developers long to discover that when it comes to creating dynamic, database-driven Web sites, MySQL and PHP provide a winning open source combination. Add this book to the mix, and there's no limit to the powerful, interactive Web sites that developers can create. With step-by-step instructions, complete scripts, and expert tips to guide readers, veteran author and database designer Larry Ullman gets right down to business: After grounding readers with separate discussions of first the scripting language (PHP) and then the database program (MySQL), he goes on to cover security, sessions and cookies, and using additional Web tools, with several sections devoted to creating sample applications.
AJAX Programming with PHP
This php pdf study material gives the details of AJAX Programming with PHP
PHP amp Zend Framework Tutorial
This php pdf study material gives the details of PHP amp Zend Framework Tutorial.PHP & Zend Framework Tutorial. I did a lot of googling, installing, re-installing, . but finally got my first php project to …
PHP-Nuke Tutorial
This php pdf study material gives the details of PHP-Nuke Tutorial..Encyclopedia - Definition of terms. Ephemerids - A “quote of the day” block. PHP-Nuke Tutorial. How to operate your PHP-Nuke website. 22603 West Main Street …
OReilly Learning PHP and MySQL 2nd Edition Aug 2007
OReilly Learning PHP and MySQL 2nd Edition Aug 2007

Latest added PHP Tutorials

Beginning PHP and MySQL: From Novice to Professional, Third Edition
A comprehensive introduction to … the scripting language PHP and the MySQL database server. This book will not only expose … core aspects of both technologies, but will provide valuable insight into how they are used in unison to create dynamic data-driven Web applications.
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
It hasn't taken Web developers long to discover that when it comes to creating dynamic, database-driven Web sites, MySQL and PHP provide a winning open source combination. Add this book to the mix, and there's no limit to the powerful, interactive Web sites that developers can create. With step-by-step instructions, complete scripts, and expert tips to guide readers, veteran author and database designer Larry Ullman gets right down to business: After grounding readers with separate discussions of first the scripting language (PHP) and then the database program (MySQL), he goes on to cover security, sessions and cookies, and using additional Web tools, with several sections devoted to creating sample applications.
The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP
Dreamweaver CS4 is a massive step forward in terms of integration with the rest of the CS4 suite (Flash, Fireworks, Photoshop, etc.), and also includes a whole host of exciting new features of its own. The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP concentrates on getting the best out of Dreamweaver CS4, rather than going into every menu item and toolbar icon. The emphasis is on developing websites compliant with the latest web standards, using CSS, JavaScript libraries (with particular emphasis on Spry, Adobe's implementation of Ajax), and PHP.
PHP and MySQL Web Development - tutorial Book
The PHP server-side scripting language and the MySQL database management system (DBMS) make a potent pair. Both are open-source products--free of charge for most purposes--remarkably strong, and capable of handling all but the most enormous transaction loads. Both are supported by large, skilled, and enthusiastic communities of architects, programmers, and designers. PHP and MySQL Web Development introduces readers (who are assumed to have little or no experience with the title subjects) to PHP and MySQL for the purpose of creating dynamic Internet sites. It teaches the same skills as introductory Active Server Pages (ASP) and ColdFusion books--technologies that address the same niche.
Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites
If you know HTML, this guide will have you building interactive websites quickly. You'll learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript, regardless of whether you already know how to program. Discover how the powerful combination of PHP and MySQL provides an easy way to build modern websites complete with dynamic data and user interaction. You'll also learn how to add JavaScript to create rich Internet applications and websites.
Learn to Implement a Shopping Cart on your website using Zend Framework
This example-driven tutorial will takes you through the process of building Model-View-Controller-based web applications. Users will learn to create and develop a storefront application on their websites.
User Interaction and Email Automation
In this tutorial we will see how Symfony can generate nice looking forms for us, before creating our own formatting class. We then progress to create a fully customized form. We will also learn about how Symfony can be expanded to use the other third-party libraries, and how can we convert a module into a fully working plugin that can be packaged up and reused in other projects.
Agile works best in PHP.
In this tutorial, we will cover the following: • Introductions to agile philosophy, including agile values and agile principles • Common problems and fears that developers face while developing a product • What is meant by agility and how it can help • Extreme programming principles • Advantages of agile process models • Team agility • Agile process models • Agile principles for the PHP project team
Magento Beginner's Guide
Get your Magento store up and running using Magento Beginner's Guide by William Rice.
Macronimous - Writing clean, secure and easy to maintain PHP code
Any code when written in a clean, easy to understand and formatted way is readily accepted and acclaimed by one and all.