PHP and graphics

This page is marked as In Progress so expect small errors or unfinished bits

PHP can do some basic tasks with images such as get the size (you will need an image on the server for this to work):

$size=getimagesize("fred.jpg");
echo "<pre>";
	print_r($size);
echo "</pre>";
        

or to extract the information about those photographs held in the files (date taken etc.):

$data=exif_read_data("fred.jpg");
 
echo "<pre>";
	print_r($data);
echo "</pre>";
        

Some of that information may be garbled or not make sense because each camera does things differently but the basics should be clear. You could use the information to label photos in a gallery perhaps.

There is also an available library of graphics functions (GD) to let you manipulate or even create images. The library is not an automatic part of every PHP server installation but is found on most. To check:

echo "<pre>";
	print_r(gd_info());
echo "</pre>";        
        

This library allows you to do much more with images. Here are a few basic examples.

Overlaying text on an image

The following code must be before any HTML or ECHOed content in your page (headers must be sent before the content of the page).

header("Content-type: image/jpeg");
$image=imageCreateFromJPEG("fred.jpg");
$textcolour=imagecolorallocate($image, 255, 0, 0);
imagettftext($image, 20, 0, 50, 50, $textcolour, "./acme.ttf", "Wow!!!");
imagejpeg($image);
imagedestroy($image);
        

The first line sends an HTTP header to the browser telling it that the following data will be an image. Then the image is copied from a file to memory. Then a colour is set up ready for use. The fourth line actually adds the text to the image using the font specified (this line is explained in more detail below). Then the image data is sent to the page for the browser to display. The final line removes the copy of the image from memory (the original is unchanged).

The fourth line (the imagettftext function call) has a lot of parameters:

  1. the image to be changed
  2. the size of font (in points) to use
  3. the angle of the text (0 meaning left to right horizontally)
  4. the position of the left edge of the first letter
  5. the position of the lower edge of the letters
  6. the colour to write the text
  7. the font to use (you need to include the ./ because if you leave it out the server will look in it's own font directory for the font and not in the current directory)
  8. the text to write

This technique could be used to mark copyright on images and for other uses. You can extend the idea in many ways including adding shadow effects by writing text twice:

header("Content-type: image/jpeg");
$image=imageCreateFromJPEG("fred.jpg");
$textcolour=imagecolorallocate($image, 255, 0, 0);
$shadowcolour=imagecolorallocate($image, 175, 0, 0);
imagettftext($image, 20, 270, 53, 53, $shadowcolour, "./acme.ttf", "Wow!!!");
imagettftext($image, 20, 270, 50, 50, $textcolour, "./acme.ttf", "Wow!!!");
imagejpeg($image);
imagedestroy($image);
        

Here the offset (starting position of the text has changed slightly between the two writes. It also writes the text top-to-bottom.

Creating thumbnails

To do this you first need to move the image into memory to work on it:

$image=imageCreateFromJPEG("fred.jpg");
		

Get the current image size:

$width=imageSX("fred.jpg");
$height=imageSY("fred.jpg");
        

Images can be different proportions so the hard part is deciding the size of the thumbnail. Normally you want a maximum size in one dimension with the other smaller. That means finding which is the largest dimension and shrinking it to the thumbnail's desired size and then shrinking the other dimension by the same amount (or the image gets distorted). There are three possibilities (the third being the image is square):

if ($width>$height) {

} elseif ($width<$height){

} else {

}
        

Complete with one way of calculating the new sizes this looks like:

if ($width>$height) {
	$thumbheight=120;
	$thumbwidth=$width/($height/120);
} elseif ($width<$height){
	$thumbwidth=120;
	$thumbheight=$height/($widtht/120);
} else {
	$thumbwidth=120;
	$thumbheight=120;
}
        

Now create a new image (the thumbnail):

$thumbnail=ImageCreateTrueColor($thumbwidth, $thumbheight);
		

and resample the original image at the new size into this new thumbnail image:

imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
		

This basically says:

Now write the thumbnail image to a suitably named file (75 is the quality/compression - from 0 to 100):

imagejpeg($thumbnail, "fredthumb.jpg", 75);
		

Now destroy the image and thumbnail held in memory:

imagedestroy($thumbnail);
imagedestroy($image);
		

To make this work copy all of these chunks in order (esxcept for the empty IF statements) and paste them into a single PHP page. Remember you need an image called fred.jpg.

To do this with all images in a directory you would need to list the files, check for images (look at the extension) and run through each image in turn. If images might be other formats (e.g. PNGs) you would also need to check for that and use different functions such as imagecreatefrompng(). Then display the thumbnails adding links to the full size images.

submit to reddit Delicious Tweet