GD Backgrounds

This tutorial concentrates on the background created using PHP and GD. You can create a palate image or a true color image. Only jpg and png are supported in the true color.
First to get a background you will need these codes. Looking at the codes, a php file opens and closes with a php call (<?php and ?>). You can also drop the php and just put <? with ?>. The image needs a global name, and in this example were using $image. All PHP calls begin with a $. It can be anything, such as $whatever. Most important every line ends with a semicolon. If you leave it out, the script will not work. PHP does not allow the slightest error in coding.
<?php
$image=imagecreate(100,50);
$red=imagecolorallocate($image, 255, 0,0);
header("Content-type: image/gif");
imagegif($image, 'red.gif');
imagedestroy($image);
?>
If you created your image using $image, then that is what you use throughout the code when allocating colors for the image or other things such as drawing on it. If you used $whatever, then $whatever is what you use. Sometimes you will need to use another parameter such as when you want to copy another image onto the background...but we'll get to that later. Going through these lines:
$image=imagecreate(100,50);The first line here is telling the server that I am creating a new image using imagecreate and the width and height are in parentheses.
$red=imagecolorallocate($image, 255, 0,0);The second line designates the color of the image, in this case red. imagecolorallocate is used for every color you want to use in the script. In other words you need to code in every color you are going to use. The first color used will end up being the background color.
header("Content-type: image/gif");The last 3 lines are required on every GD image. The header tells the server what type of image you are creating (in this case a gif). You can create a png using image/png, or a jpg using image/jpeg. GD reads it in as jpeg, but will write it out as jpg.
imagegif($image);The next line outputs the image to the browser or file. To create a gif file you would name the image on this line, such as imagegif($image, 'red.gif'); In this case it will no longer be visible to the browser but the image will be in your directory. Again you can use png or jpg if that is in your header.
imagedestroy($image);The last line destroys the image and frees up any memory from the server created by the image.
Here are the samples for comparison.
png
jpg
gif
According to the PHP GD Manual imagecreatetruecolor() returns an image identifier representing a black image of the specified size. Unlike imagecreate() it will not fill the back with the first color designated. To fill the back with color, you would have to either draw on it or use
imagefill($image, 0,0,$red);
where 0,0 is the top left corner pixel and $red is the color you allocated. These come out looking like:
true color jpg
true color png
You can use imagecreatetruecolor with a gif if you add this line at the bottom just before the header.
imagetruecolortopalette($image, false, 255);
The false means you do not want to add a dither. The 255 is how many colors you want to allow in your image (255 covers all the colors available).
truecolor false dither gif
truecolor true dither gif
Lastly, for transparent back you need to use either gif or png. In either case, the line just below the background color should be:
imagecolortransparent($image, $red);
For a truecolor image you are best to use black because black is the color allocated by the truecolor palate. Also imagecolortransparent() can only be used one in any image. If it is used twice, the second color will be transparent and the first color will revert to a full color. If you want to use a black color in your image you should use an almost black such as:
$almostblack=imagecolorallocate($image, 1,1,1);
There are ways to make other colors transparent or near transparent using
imagecolorallocatealpha($image,255,0,0,127);
Where 127 is completely transparent. You can use 1-127 for the alpha. Also there are other calls like
imagesavealpha($image,true/false);
but we will not go into that here.
One last note is that you can comment out lines that you write to yourself or want to check the image without a certain line of code. If the line is short you can use 2 backslashes in front of the line. You can also use a hash mark # in front of a line. If you want to block out a whole set of codes for the server to ignore then you can use /* before the first line and */ after the last line.
Right click, select all, ctrl+c to copy.