Article

Build An Automated PHP Gallery System In Minutes

Page: 1 2 3 4 5 6 7 8 Next

Using the GD Library

The Graphics Development Library or GD Library also provides us with the ability to create and modify images. It supports JPEG, BMP and PNG in its standard form; though some versions of GD also support GIF and TIFF. The latest versions of the GD Library can be downloaded from www.Boutell.com/gd.

PHP provides us with GD-specific functions for creating and modifying particular types of the images. Therefore, before creating thumbnails for the uploaded images we need to find out exactly which functions are to be used.

To produce these thumbnails, we need to:

  1. Create an image handle to read the uploaded image
  2. Create another image handle that will be used to create the Thumbnail
  3. Resize the original image via its handle
  4. Save the resized image with the Thumbnail handle

The functions required in Steps 1 and 4 are image type-dependent and their naming convention is such that they can easily be recognized, e.g. ImageJPEG, ImageGIF. The suffix in each function name corresponds to the type of image the function can handle. The following array lists the suffix we’ll use to call these specific functions:

$gd_function_suffix = array(      
 'image/pjpeg' => 'JPEG',    
 'image/jpeg' => 'JPEG',    
 'image/gif' => 'GIF',    
 'image/bmp' => 'WBMP',    
 'image/x-png' => 'PNG'    
);

In order to call ImageJPEG, we’ll create its name dynamically before calling it:

// Get the Name Suffix on basis of the mime type    
$function_suffix = $gd_function_suffix[$filetype];    
   
// Build Function name for ImageCreateFromSUFFIX    
$function_to_read = 'ImageCreateFrom' . $function_suffix;    
   
// Build Function name for ImageSUFFIX    
$function_to_write = 'Image' . $function_suffix;

The above code will assign the variable the following values in the case of a JPEG file being uploaded:

  • $function_to_read => ImageCreateFromJPEG
  • $function_to_write => ImageJPEG

Version 1.x.x

Version 1.x.x has its limitations; it can only create new images in, at most, 256 colors. Therefore, while putting together the code snippets given above, we will use GD 1.x.x-specific resizing functions. The functions are:

  • ImageCreate
  • ImageCopyResized

Resizing with GD 1.x.x

$function_suffix = $gd_function_suffix[$filetype];    
$function_to_read = 'ImageCreateFrom' . $function_suffix;    
$function_to_write = 'Image' . $function_suffix;    
   
// Read the source file    
$source_handle = $function_to_read($images_dir . '/' . $filename );      
           
if ($source_handle) {    
 // Let's create a blank image for the thumbnail    
 $destination_handle = ImageCreate($thumbnail_width, $thumbnail_height);    
   
 // Now we resize it    
 ImageCopyResized($destination_handle, $source_handle,    
   0, 0, 0, 0, $thumbnail_width, $thumbnail_height,    
   $size[0], $size[1]);    
}    
   
// Let's save the thumbnail    
   
$function_to_write($destination_handle, $images_dir . '/tb_' . $filename);    
ImageDestroy($destination_handle);

The most interesting aspect to note in the above code is how the image is resized using ImageCopyResized function. This function requires 10 arguments; let me list them one by one:

  • $destination_handle holds the information regarding the blank image we created equivalent to the size of the thumbnail.
  • $source_handle holds information relating to the source file, which was the originally uploaded image.
  • Arguments 3 to 6 hold the starting coordinates from which the image should be resized. Because we require a thumbnail, and a thumbnail is just a smaller version of a full-sized image, we use 0,0 as our starting coordinates.
  • Arguments 7 and 8 hold the destination image dimensions, i.e. the dimensions of the thumbnail in our case.
  • Arguments 9 and 10 hold the source image dimensions, which in our case are the dimensions of the original image.

Version 2.x.x

Version 2.x.x isn’t restricted to using 256 colors, but the procedure employed is not very different from the one described for version 1.x.x code. Only here, we have to use GD 2.x.x specific resizing functions instead. These functions are:

  • ImageCreateTrueColor
  • ImageCopyResampled

Resizing with GD 2.x.x

$function_suffix = $gd_function_suffix[$filetype];    
$function_to_read = 'ImageCreateFrom' . $function_suffix;    
$function_to_write = 'Image' . $function_suffix;    
   
// Read the source file    
$source_handle = $function_to_read($images_dir . '/' . $filename);    
           
if ($source_handle) {    
 // Let's create a blank image for the thumbnail    
 $destination_handle =    
   ImageCreateTrueColor($thumbnail_width, $thumbnail_height);    
   
 // Now we resize it    
 ImageCopyResampled($destination_handle, $source_handle,    
   0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);    
}    
   
// Let's save the thumbnail    
$function_to_write($destination_handle, $images_dir . '/tb_' . $filename);

Everything remains the same as with the version 1.x.x code, except for the two different functions used:

  • in place of ImageCreate, we used ImageCreateTrueColor.
  • in place of ImageCopyResized, we used ImageCopyResampled.

You can read more about GD functions in the PHP manual at www.php.net/gd.

Now, putting it all together will yield you upload.php. The file is part of the archive you can download here.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links