Easy thumbnail generation using PHP

One of the frequent requirements in web design is that of thumbnail generation. Although there are many libraries out there I found PHP Thumb simple and quick enough to implement in a recent project. PHP Thumb is a light-weight image manipulation library aimed at thumbnail generation. It offers the ability to resize, create custom crops, or square crops from the center, and rotate the image. If the need arises you can also easily add custom functionality to the library through plugins.

phpthumb uses the ‘gd’ library to process the images, so you will need to have gd installed on your system. You can use phpinfo() or the following to quickly check for the same:

<?php
var_dump(gd_info());
?>

If ‘gd’ library is not installed the above will return a ‘Call to undefined function gd_info()’ error.

Simple examples

The following simple code shows how to quickly resize a image.

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
/* Resize to 100x100 pixels */
$thumb->resize(100, 100);
$thumb->show();

..rotate a image.

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
/* Rotate the image  by 180 degrees */
$thumb->rotateImageNDegrees(180);
 
$thumb->show();

..crop a image.

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
/* $startX, $startY, $cropWidth, $cropHeight */
$thumb->crop(50, 50, 100, 100);
 
$thumb->show();

..resize by a percentage.

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
/* Percentage can be less than or greater than 100 */
$thumb->resizePercent(60);
 
$thumb->show();

…or save it to a different format. Formats can be any from the following:
‘GIF’, ‘JPG’, ‘PNG’.

require_once '../ThumbLib.inc.php';
 
$image_path = 'images/';
 
$thumb = PhpThumbFactory::create($image_path  . 'test.jpg');
$thumb->resize(300, 300);
 
$thumb->save($image_path  . 'test.png', 'png');

Thumbnail options

While creating a thumbnail you can pass various options in an array; for example, to scale an image up to the desired dimensions and to set the jpeg quality to 60 we will use the following code.

require_once '../ThumbLib.inc.php';
 
$options = array('resizeUp' => true, 'jpegQuality' => 60);
 
$thumb = PhpThumbFactory::create('images/test.jpg', $options);
$thumb->resize(300, 300);
$thumb->show();

More options can be found here and documentation regarding other functions can be found here.

Custom plugins

phpthumb also lets you create custom plugin for further image processing. A sample plugin for reflection is provided in the download code, a example of which is shown below:

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
/* Params: $percent, $reflection, $white, $border, $borderColor */
$thumb->createReflection(40, 40, 10, true, '#a4a4a4');
$thumb->show();

Chaining functions

To quickly apply various formatting to the image we can chain various functions as shown below. The following code will crop an image, resize it to 150% of the original size and then add a reflection to the final image.

require_once '../ThumbLib.inc.php';
 
$thumb = PhpThumbFactory::create('images/test.jpg');
$thumb->crop(93,58, 40, 40)
      ->resizePercent(150)
      ->createReflection(40, 40, 80, true, '#a4a4a4');
 
$thumb->show();

The output of the above is shown below.



5 thoughts on “Easy thumbnail generation using PHP

  1. This image looks neat. I would like to see a demo for images where there is a minor change in color in adjacent pixels. Such as orange text over a red background etc. This is where we can actually test a thumbnail code effeciency.

  2. I tried this but in the downloaded phpThumb zip is no file called ThumbLib.inc.php

    nor is there a class called PhpThumbFactory

    Is this documentation to old, please remove it if it is, very frustration

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>