Thumbnails creation

It can sometimes be useful to have images manipulation functions. The function described below allows the creation of thumbnails in PHP.

This function is compatible with three "web" file formats, JPEG, GIF and PNG.

The documentation for the class containing this function is available through this URL:
http://www.lapinbleu.ch/phpdocumentor/Graphical/GFXimages.html

Use

First of all, it is necessary to instantiate the class as an object

<?php

$oImage = new GFXImages();

?>

We must now give parameters to the object so it can perform the requested task:

<?php

$oImage = new GFXImages();
$oImage->filename = "path/to/filetoreduce.jpg";
$oImage->thumb_dir = "thumbnaildirectory";
$oImage->thumb_filename = "fooname.jpg";
$oImage->thumb_height = 100;
$oImage->thumb_width = 80;
$oImage->jpeg_quality = 60;

?>

 

The parameters are:

  • filename: path and filename to resize.
  • thumb_dir: folder where the thumbnail is stored
  • thumb_filename: filename of the thumbnail
  • thumb_width: width of thumbnail
  • thumb_height: height of thumbnail
  • jpeg_quality: quality used for JPEG compression. If the file is not a JPEG, this parameter is not used.

The only thing remaining now is to call GFXimages::createGFXimageThumb() function.

<?php

$oImage = new GFXImages();
$oImage->filename = "path/to/filetoreduce.jpg";
$oImage->thumb_dir = "thumbnaildirectory";
$oImage->thumb_filename = "fooname.jpg";
$oImage->thumb_height = 100;
$oImage->thumb_width = 80;
$oImage->jpeg_quality = 60;
$oImage->createGFXimageThumb(); 

?>

From that moment, the "thumbnaildirectory/fooname.jpg" file is created and its size is 60 pixels by 80 pixels. It is the possible to use the following code to call the resulting file:

<?php

echo "<img src=\"" . $oImage->thumb_dir . "/" . $oImage->thumb_filename . 
    "\" width=\"" . $oImage->thumb_width . "\" height="\" . 
    $oImage->thumb_height . "\" />";

?>

Alternatives

There are alternative web solutions as urlimg.com. Although these solutions are very quickly put in place, there are not always a panacea since they show their limits very quickly. Here are the disadvantages of a third party solution like this:

  1. Dependence on a third party. If the service fails or is no longer provided, the site must be completely rethought.
  2. Images stored on a third party server, resulting in increased traffic and a slowdown of the information display.
  3. Loss of available resources use (unused disk space, ... ).
  4. Update process relatively slow (around 2 or 3 hours for urlimg.com). Moreover, it is impossible to control this update.

GFXimages::createGFXimageThumb() integration is not longer than integrating a third party site solution. It may be judicious taking time to study the specifications in order to choose the right solution.