Image Copy with PHP and GD
Lesson 2: File types

JPGPNGGIF
Here are examples of the three file types. The JPG and PNG are true color images. The JPG is from the code of Lesson 1. To get the PNG simply change the Header to png or gif and the output to png or gif. header("Content-type: image/png"); imagepng($im); The GIF however needs further change in the code. See below.
In order for your file type to work properly you need to decide whether it will be a true-color or a palette-color image. The line of code: $im=imagecreatetruecolor($W, $H); will only work for JPG and PNG. What it does is automatically inserts a black background and then renders true color images on that background in an unlimited number of colors. $im=imagecreate($W, $H); Will give you a palette of 255 colors. With palette colors you need to add a background to your script. Using the following codes replace the imagecreatetruecolor for palette colors, with these lines:
$im=imagecreate($width, $height);
$im=imagecreatefromjpeg('black.jpg');

Here are some black backgrounds:
JPGPNGGIF
JPG
true
JPG
Palette
JPG
BG
The first JPG is a true color. The second JPG is a palette color with no background. The third is a palette color with a black jpg background.
PNG
True
PNG
Palette
PNG
jpg BG
PNG
png BG
GIF
Palette
GIF
jpg BG
GIF
gif BG+Girl
The gif image does not render well with a jpg background. The colors bleed or merge. I also had to change the girl image to a gif in order to get the proper colors to show.
So as you can see you need to decide ahead of time whether you want a JPG, PNG, or a GIF image. In some instances you will have to change all of your images to the filetype that you have chosen to use in order to get the color you want. Sometimes a mix of image types will work and sometimes they just won't and you will have to take your image to IM and change its type. PNG and GIF images will allow a transparent background. JPG will not. The Header and the output type are what controls your final image type in the script.

Now that you have all the basics for using image copy, lets have some fun with the codes in Lesson 3.

LESSON 3