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.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

3 Responses

1

Maxim Dsouza

March 31st, 2011 at 11:10 am

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

العاب

June 5th, 2011 at 6:37 am

it’s very easy class i like it i will use it to create thumbnils in my site it’s very easy
thank you

3

Diego

May 28th, 2014 at 8:57 am

Exelent!! thanks!!

Your thoughts