Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Dynamic Graphs with PHP, MySQL and GD
Basic Image Dynamics
Before we jump into content driven graphs, lets try some simple image manipulation. Create a new php file and name it image.php. You can check on your progress at anytime by entering the php file as a URL in your browser (ex: http://127.0.0.1/image.php) This file is going to be the image. I can already hear people saying "But its just text! How can it be a picture?" Its all in the first line of code:
header ("Content-type: image/png");
This tells the browser that this file is, in fact, an image (as compared to a "text/html" document). This line has to be present in every image generated by PHP/GD, and it should be as is, although the "image/png" can be changed to "image/jpeg" if you want (I encourage using PNG's, however, as their quality is superior to JPEG's). Now the next line:

<?php
$im
= imagecreate (300, 300);
?>
imagecreate is a new function that is only available with GDLib. It creates a blank image with the specified dimensions. Now, we need to allocate some colors that were going to use in the image. To do this we use the imagecolorallocate function:
$blue = imagecolorallocate ($im,0,0,255);
$im was used as reference to which image the color will be allocated to (you can work with more than one image at a time). Most image functions require you to specify which image you are operating on. The three integers that follow are the color code in RGB (NOT hexadecimal). Interestingly enough, and this is something I haven't read anywhere, the first color you allocate becomes the background. If no colors are allocated, then the image takes on a black background. So far, our 300x300 image has a blue background. Put another imagecolorallocate function before it allocating white (255,255,255) so that you get a nice subtle white background. Lets take a look at our code so far.

<?php
header
("Content-type: image/png");
$im = imagecreate (300, 300);
$white = imagecolorallocate ($im,255,255,255);
$blue = imagecolorallocate ($im,0,0,255);
?>
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
dont forget the last line (top of page2)steve05/02/05 18:46
Just a small thing to keep an eye onAmitabh Kant09/25/04 09:17
RE: Good GD exampleTran PHuoc Long12/19/03 10:18
Good GD exampleRupa08/14/03 05:46
RE: Why don't youandy06/30/03 03:31
even easier....Roger02/27/03 17:42
Why don't youRob02/21/03 13:41
Good GD use, poor SQL practice!kev02/21/03 12:32
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.