How to create thumbnail images for your Web pages

Create thumbnails for web pages can be done with a PHP program installed on the server, coupled with a upload form, but to automate the creation, and control the image quality, it is better to install a script locally. It will be easier to have more efficient graphics functions.

Installing the PHP graphics library

PHP version 4 or 5 uses the library gd2 for image processing, which is not enabled by default (although it could be on the server, use phpinfo to know).

To install the extension gd2 graph:

  1. Copy in the /php/ directory php.ini-recommanded to php.ini.
  2. Define the path of extensions in the extension_dir variable, for example: extension_dir = "c:/php/ext.
  3. Enable in the list of extensions php_gd2.dll (or under Linux php_gd2.so) by removing the semicolon at the beginning.

Now to verify that gd is active, launch the program that is provided byt the PHP manual:

<?php
$array=gd_info ();
foreach ($array as $key=>$val) 
{
  if ($val===true)  { $val="Enabled"; }
  if ($val===false) { $val="Disabled"; }
  echo "$key: $val \n";
}
?>   

It displays the supported formats, or an error message if the extension is not enabled.

Creating a thumbnail

PHP allows to create good quality thumbnails.

Defining the parameters

$oldname = "girl.jpg";
$newname = "thumb.girl.jpg";
$newh = 240;

We have chosen a height of 240 pixels height for the thumbnail. But wa can rather define a new width: $neww = xxx;

Interpolating dimensions

$size = getImageSize($oldname);
$w = $size[0];
$h = $size[1];
$neww = intval($newh * $w / $h);

If we chose to define the width rather than height, the formula will be: $newh = intval ($neww * $h / $w);

Re-creating an image of smaller size

$resimage = imagecreatefromjpeg($oldname); 
$newimage = imagecreatetruecolor($neww, $newh);  
imageCopyResampled($newimage, $resimage,0,0,0,0,$neww, $newh, $w, $h);

The functions were selected for the quality of the outcome. Under some configurations and versions of PHP they could not be available, you have to find in the manual equivalent functions, but the result may be of lower quality.

Saving the new image

imageJpeg($newimage, $newname, 85);

Likewise for the type of image, choose the appropriate function for the file format. In the case of a jpeg file, the third parameter is the quality of restitution (in inverse proportion of the compression level), with a maximum of 100 with no compression.

Getting a better image quality

In our example, with a picture in comic book style, it turns out that the thumbnail is as sharp as the original design. With other types of images, photos, for example, if you are not satisfied, you can use the G'MIC plugin for GIMP.

To use this software:
1) Download and install the free software GIMP.
2) Download and install G'MIC.
3) In the Filter menu, select Enhancement. Try then the options.

Note that the G'MIC plugin is particularly effective in improving the quality of images to restore, blurred or dashed photos.

Comparison of results

The first thumbnail is performed by G'MIC, the second by PHP with the imageCopyResampled function.

Source code

Part 2: Building a gallery of thumbnails of same size in PHP.

Getting the software