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 300×300 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);
?>