TechniqueUse the ImageCreate() function and then use the ImagePng() , ImageJpeg() , or ImageGif() function, depending on your system: <?php header("Content-type: image/gif"); $im = ImageCreate(100,20); $red = ImageColorAllocate($im, 255, 0, 0); $white = ImageColorAllocate($im, 255, 255, 255); ImageString($im, 3, 3, 3, "YES!", $white); ImageGif($im); ImageDestroy($im); #Free memory ?> CommentsIf you run the preceding script, you should see something like the following (although the font might be different): YES! But what exactly are we doing? Let's go over this line by line:
In the example, we use the ImageGif() function to convert the image, $im , to a GIF and then output it to the browser ( STDOUT ). However, if we want to save it to a file, we can provide an optional second argument, like so: <?php $filename = "some_image.gif"; $im = ImageCreate(150,40); $red = ImageColorAllocate($im, 255, 0, 0); $white = ImageColorAllocate($im, 255, 255, 255); ImageString($im, 3, 4, 3, "Saved to $filename with GD", $white); ImageGif($im, $filename); ImageDestroy($im); # Free memory associated with image ?> The image was written to <?php echo $filename; ?> |