Creating An Image With the PHP GD Library

The three standard type of images that can be created from scratch with the PHP GD Library are: JPG, GIF and PNG.

JPG Is Designed to Compress Full-Color Images, & Is Ideal For Digital Photos, Etc.
GIF Is Designed to Support Only 256 Colors, Along With Transparency, Interlacing & Animation
PNG Is Designed As An Alternative to GIF, With the Same Basic Features, But Does Not Support Animation

The header() function is used to tell the browser which content type it will be sent. This must be specified before any other output is sent to the browser, whether it is blank lines, PHP code, HTML tags, etc. The header options for images are:

header('Content-type: image/jpeg');
header('Content-type: image/gif');
header('Content-type: image/png');

Now we can use the imagecreate() function to create a blank image, assign it a width and height, and store it in a variable. The syntax is: imagecreate(width, height)

So far, our code might look like this:

  header('Content-type: image/png');
  $png_image = imagecreate(150, 150);

Using the imagecreatetruecolor() function will create a black image (instead of a blank image) with your specified width and height, which you can then see against a white background.

Alternatively, the next step would be to specify the background color of the image, or fill it, using the imagecolorallocate() function. The syntax is: imagecolorallocate(image, red, green, blue)

"Red", "Green" and "Blue" indicate where the values of these color components should be specified. The parameters allow integers between 0 and 255, or hexadecimals between 0x00 and 0xFF.

  header('Content-type: image/png');
  $png_image = imagecreate(150, 150);
  imagecolorallocate($png_image, 15, 142, 210);

Alternatively, the imagecolorallocate() function is used to specify a color and store it in a variable, while the imagefilltoborder() function is actually used to flood fill the entire image with color.

If you tried out either of the above examples, you received an error, didn't you? That is because the code was not complete. To complete the code, we can now send our image to the browser. Three functions are available for this purpose, one for each type of image.


Afterward, we can clear up the memory that is being taken up by storing the image. The imagedestroy() function us used for this purpose.

  header('Content-type: image/png');
  $png_image = imagecreate(150, 150);
  imagecolorallocate($png_image, 15, 142, 210);

And there we have it, our image is now created, colored and sent to the browser. Try it out!

See Example

Note: To save an image to the server on the fly, use the image function's second parameter to specify a location and filename.

  $path_image = 'saved-example.png';
  imagepng($png_image, $path_image);


Function Description
header() Send a Raw HTTP Header
imagecreate() Create a New (Blank) Palette-Based Image
imagecreatetruecolor() Create a New True-Color (Black) Image
imagecolorallocate() Allocate a Color For An Image
imagefilltoborder() Flood Fill to Specific Color
imagejpeg() Output a JPEG/JPG Image to Browser or File
imagegif() Output a GIF Image to Browser or File
imagepng() Output a PNG Image to Browser or File
imagedestroy() Destroy An Image