Work with pictures

There are more possible ways how to work with graphics, but the most common is the GD library.

We have to create a virtual picture first. We can create an empty picture (using the imagecreatetruecolor function), or we can start with some picture from a file in e directory or somewhere on the web (for example the imagecreatefromjpeg function). In the second case, the function has different result, that is used in most of the GD function; because we need an address instead of a resource, we have to use a "@" directive. Using both methods, we will make a memory reservation for the picture depending on the picture size (and color depth). All graphics operation outside the declared picture are ignored (for example, part of a circle), but most important, in distinction of some other languages, it cannot cause any problems with writing outside the memory reserved for the picture.

We can use a line drawing, text writing and another graphics function for this virtual image, till you decide to generate them to an output. Please remember, that everything will be done on the server, so for example fonts have to be installed on the server.

After you have the picture prepared, you can stream it directly to output (the first example today), or generate a file on the server (both using one of imagejpeg, imagegif or imagepng function). In the case of stream, the function should correspond with the http header, as is defined before picture generating.

If you generate directly stream instead of saving picture on the server, your php script should have a different structure - no html headings, only php functions without any "echo" or "print" command, and even http header should be set to "image/type" (we didn't change it yet; default value is "text/html"). Then, we can use this php script as an image:

<img src="demo.php">

The following example is from the php manual ( Note, that all the GD function uses the virtual picture address as a parameter - we can work with more pictures simultaneously. Another note, all the colors should be defined before use. The reason is, that in the time of this library appears, most pictures had no more than 256, so it use "color palette" - table with list of all colors used. For the RGB color definition, the color components in this example are written in the hexadecimal code - its' entry starts with the 0x letters, following two hexadecimal letters, which ale 0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F. The 0x00 is zero, the 0xFF is 15*16+15=255 - in case of color, this is the maximum lighting. You can try for example change the line

$black imagecolorallocate($im0x000x000x00);


$black imagecolorallocate($im0xFF0x000x00);

and watch, what will happened (R-G-B as FF-00-00 should create red color).

The first example:

// Virtual image creating with a resolution of 200x100:
$im imagecreatetruecolor(200100);

// The color preparation
$white imagecolorallocate($im0xFF0xFF0xFF);
$black imagecolorallocate($im0x000x000x00);

// Fill all the background using filled rectangle
// (there is error in the manual, 299 should be 199)

// All the line drawing objects should be 5 dots thick

// The rectangle line drawing

// The picture output - without any name, the function will
// generate output directly to users explorer window"
header('Content-Type: image/png');

// Storage dispozition (memory freeing)

Recapitulation: Direct picture generation has no html headers and there must be no "echo" or "print" command, so we cannot use debugging messages.

On the next example, we will start with an existing image. If you like to use image in your directory, you have to save there one as the first. For example, nice picture are here (our local copy). The result we can show using

<img src="bears.php">

And here is our bears.php script:

header('Content-Type: image/jpeg');
$img = @imagecreatefromjpeg('bears.jpg');
$tmp = imagecreatetruecolor(320,200);

The header now define, that output will be jpeg, so the function imagejpeg has to be used. Two virtual images are created. For the copy of the image, the imagecopyresampled function is used. Note, that it can both increase and decrease the picture size. The imagesx (image-size-x) will return the horizontal image size. The rest is the same.

The next example will write the result to the file as well as to stream:

header('Content-Type: image/jpeg');
$src = @imagecreatefromjpeg("data/".$_GET["file"]);
$dst = imagecreatetruecolor(100,75);
imagecopyresampled($dst, $src, 0, 0, 0, 0, 100, 75, imagesx($src), imagesy($src));

It will work with an image, which have to be in the data directory before use. This picture should be called using the GET method, with the "file" variable set, for example

<img src="test.php?file=bears.jpg">

Now, try some of other function from the list.

Another possibility is to create pictures (mainly graphs) on the client side. Because there is no javascript support for graphics, you would have to use the java applets for this. This is out of range of this course.