In this tutorial I’m going to tell you how to create simple images from PHP with help of GD library in just a snapshot. If you don’t have GD library installed you can download it: GD Lib

In PHP creating a simple image requires a number of functions. I’ll step through them one by one.

Output a header containing the MIME type of the image that you’re creating–in this case, a PNG.

1
  header ("Content-type: image/png");

Create a variable to hold the blank image you’ll make using

1
  ImageCreate();

This function requires a size in pixels. The format is

1
  ImageCreate(x_size, y_size);

so for a 250-by-250-pixel image, you’d use:

1
  $newImg = ImageCreate(250,250);

Because your image is blank, you’re going to want to fill it with a color of some sort. However, first you need to allocate a name for a color by its RGB values, using the

1
  ImageColorAllocate();

function. The format for this function is

1
  ImageColorAllocate([image], [red], [green], [blue]);

For a nice sky blue color, you’d use:

1
  $skyblue = ImageColorAllocate($newImg,136,193,255);

Next, you need to fill the image with this color using the

1
  ImageFill();

function. There are actually several versions of

1
  ImageFill();

such as

1
2
  ImageFillRectangle();
  ImageFillPolygon();

and so on. For simplicity’s sake, we’ll just use

1
  ImageFill();

to do a flood file, with the following format:

1
2
  ImageFill([image], [start x point], [start y point], [color]);
  ImageFill($newImg,0,0,$skyblue);

In last, you have to create the final image and destroy the graphics stream to free up memory and clean up after yourself:

1
2
  ImagePNG($newImg);
  ImageDestroy($newImg);

Your code should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
  header ("Content-type: image/png");
 
  $newImg = ImageCreate(250,250);
 
  $skyblue = ImageColorAllocate($newImg,136,193,255);
 
  ImageFill($newImg,0,0,$skyblue);
 
  ImagePNG($newImg);
 
  ImageDestroy($newImg);
 
?>

If you call this script blue.php and access it with your browser, you should see a 250-by-250-pixel, sky blue PNG.

Learn PHP Graphics in 10 Minutes

You can also use image creation functions to manipulate images, as in creating a thumbnail of a larger image.

Suppose you have an image from which you want to make a 35-by-35-pixel thumbnail. What you’ll be doing is creating a new image that is 35 by 35 pixels in size; making a graphics stream containing the contents of the original image; and then placing a resized version of the original image into the new, empty graphic.

The key function used to achieve this is

1
  ImageCopyResized()

which requires a format like this:

1
2
3
4
5
6
  ImageCopyResized([new image handle],
  [original image handle],[new image X],
  [new Image Y], [original image X], 
  [original image Y], [new image X], 
  [new image Y], [original image X], 
  [original image Y]);

Comments are in the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<? 
 
  /* send a header so that the browser knows
   the content-type of the file */
  header("Content-type: image/png");
 
  /* set up variables to hold the height and
   width of your new image */
  $newWidth = 35;
  $newHeight = 35;
 
  /* create a blank, new image of the given new
   height and width */
  $newImg = ImageCreate($newWidth,$newHeight);
 
  /* get the data from the original, large image */
  $origImg = ImageCreateFromPNG("test.png");
 
  /* copy the resized image. Use the ImageSX()
  and ImageSY functions to get the x and y sizes
  of the orginal image. */
 
  ImageCopyResized($newImg,$origImg,0,0,0,0,
  $newWidth,$newHeight,ImageSX($origImg),
  ImageSY($origImg));
 
  /* create final image and free up the memory */
  ImagePNG($newImg);
  ImageDestroy($newImg); 
 
?>

If you call this script resize.php and access it with your browser, you should see a 35-by-35-pixel thumbnail PNG. That’s all in this Graphics session.