Create drawings with PHP GD graphic commands.
Drawing graphics with PHP graphic methods will let you do anything you can do with any graphic design or graphic animation program. Because the PHP program is an open source language, you get the added benefit of not having to purchase graphic design or animation software. If you want to generate graphics for your web site drawing graphics with PHP is just a simple matter of learning a few PHP graphic commands. Once you know these basic commands you can then build graphic PHP scripts that can be used in your web page and your other graphic programs.
Difficulty:
Moderate
Things You'll Need:
*.Web hosting provider with PHP support
Instructions
1.
Create a new file in your text editor and type in the code below to allow access to the PHP software on your web hosting service providers server. 2.
Type the code, below, into the next line of your text editor to define a 400 pixel by 400 pixel drawing area (variable $firstDrawing) on your web page where objects will be drawn $firstDrawing = ImageCreate(400,400);
3.
Type the code, below, starting at the next line in your text editor to define the colors (black, white, red, green and blue) that will be used to color different objects you will draw in this PHP file. $black = ImageColorAllocate ($firstDrawing, 0, 0, 0); $white = ImageColorAllocate ($firstDrawing, 255, 255, 255); $red = ImageColorAllocate ($firstDrawing, 255, 0, 0); $green = ImageColorAllocate ($firstDrawing, 0, 255, 0); $blue = ImageColorAllocate ($firstDrawing, 0, 0, 255);
4.
Type the code, below, starting at the next line in your text editor, to draw a red rectangle with its upper left corner positioned at the web page coordinate (15, 15) with a width of 55 pixels, a height of 85 pixels and drawn and to draw a white rectangle that an upper left hand corner placed at the web page coordinate (55, 85) with a width of 125 pixels and a height of 135 pixels. ImageRectangle($firstDrawing, 15, 15, 55, 85, $red); ImageRectangle($firstDrawing, 55, 85, 125, 135, $white);
5.
Type the code, below, starting at the next line in your text editor to draw green ellipse that is positioned at the web page coordinates (200,200), has a width of 125 pixels and height of 135 pixels. ImageEllipse($firstDrawing, 200, 200, 125, 135, $green);
6.
Type the code, below, starting at the next line in your text editor, to define the file that to inform the server the type of graphic file that is to rendered (a PNG file), place the PNG drawing on the web page and to free memory resources used to store your graphic file(imageDestroy) header ("Content-type: image/png"); ImagePNG($firstDrawing); ImageDestroy($firstDrawing);
7.
Type the code, below, starting at the next line in your text editor to inform the web server that the PHP script has ended and return to normal program flow. ?>
8.
Copy and paste the PHP script code, below, if you want to ensure that the PHP drawing code is error free.
9.
Save your text file with a "php" extension and upload it to a location in your website directory. Enter the URL file you have assigned to the the PHP file into the URL text box in your browser. Observe that the the page draws the two rectangles and the ellipse you specified in the PHP file.
A couple of notes, saztiine your request variables before pulling data directing into you code. Avoid using double quote for simple string echo as it is extra load on php parsing use single quote instead. Play with MVC type frameworks such as Zends framework to get familiar with design patterns. Nice tutorial for beginners but be warned, I had to fire a bunch of people recently who programmed like this go back to programming basics and learn popular framework methodologies.
ReplyDelete