The four essential parts to creating an image are:
header() is required when you are outputting the image to a browser. If you are writing the image to a file instead, you don't need it. header() has to be before any output is generated. If the script does something that doesn't generate output to a browser; editing a log file or getting the variables inputted from a form for example, that can be before header() but anything that manipulates the image has to be after header(). The format for header() is:
header( "Content-type: image/png" );
If your server supports gif's and/or jpg's you can use them instead.
If you are creating an image from scratch, you must create a special kind of variable called an image resource. imagecreate() is one way to create an image resource. An image resource is a variable that stands for the image you are manipulating and is used in image functions. imagecreate() has two arguments, the width and height of the image in pixels. To produce an image 200px wide and 50px high, you use this format:
$image = imagecreate( 200, 50);
After you have created your image and did whatever manipulation you want, you output it with imagepng() or imagegif() or imagejpeg(). You can also use imagewbmp() but WebTV won't be able to see the results. All three have a require an image resource as an argument and have an optional file path argument. If you leave out the file path, the image is outputted directly to the browser. If you supply a file path, PHP will attempt to write the data to a file instead. imagejpeg() has a third optional argument that controls the quality that ranges from 0 to 100. The format to out put an image to a browser looks something like this:
imagegif($image);
and the format to write to a file looks something like
imagegif($image, "images/test.gif" );
That will write the image to the file test.gif in the directory images. For this to work, images/test.gif must already exist. If it doesn't you can use touch() to create it. Permissions must be set to 666 to write to a file.
imagedestroy() frees up any memory associated with an image resource. Its argument is the image resource you are working with. The format is something like:
imagedestroy($image);
To create and output a blank 200x50 gif to the browser, put all the examples above together:
header( "Content-type: image/gif" );
$image = imagecreate( 200, 50);
imagegif($image);
imagedestroy($image);
You are not limited to creating images from scratch, you can also existing images. If the image ia an gif, jpg or png, use imagecreatefromgif(), imagecreatefromjpeg() or imagecreatefrompng() to make an image resource from it.
To make an image resource from the image above, I'ld use this format:
$image = imagecreatefromgif("images/rainbow.gif");
|
|
|