phpThumb – Generate Thumbnails on the Fly

Different sizes of the same image are often required on different webpages of a website. At some places, you might show full size image while other webpages will need a thumbnail size display. Working with image sizes can be a challenge because of the calculations involved for maintaining aspect ratios. You might want to resize an image either on the basis of its width or height. The phpThumb() is a popular PHP library for resizing and cropping images on the fly.

The phpThumb() uses the GD library to generate thumbnails from all popular image formats. It supports JPEG, PNG, GIF, BMP, etc. For basic functionality with fast performance, you can also use phpThumb() with ImageMagick. However, you would require GD library functions for using the complete power of phpThumb().

Resizing Images with phpThumb()

You can choose the full image or a portion of the image as the source for the phpThumb(). The output will be smaller or even larger image of the selected source. The size of the output image can be configured.

The source for phpThumb() can be a local file, remote file or a file in the database. Multiple sizes of any source image can be generated and cached separately. This helps in reducing the server load. Importantly, if the source image is a local file, then the thumbnails are automatically updated when source image is modified. Images can be cropped either by a specified number of pixels or by a percentage of original image.

Other Useful Features

The phpThumb() is more than resizing images. You can rotate the image either by an arbitrary angle or auto-rotate to either portrait or landscape orientation. In addition, the quality can be auto-adjusted to fit a certain output byte size. Provided you have GD library v2.0+, PHP v4.3+ and ImageMagick (optional for certain features), you can apply many filters to the image. Some of the filters are:

  1. Image can be watermarked by another image or text.
  2. Border or frame can be added to the output image.
  3. Control contrast and brightness levels.
  4. Blur images and drop shadows.
  5. Rounded Corners
  6. Flip image either horizontally or vertically

Few other filters are also available.

Sample Use

The images are displayed using the HTML img tag. The src attribute of the img tag specifies the location of image. To use the phpThumb(), call the phpThumb.php file in the src attribute.  Pass the actual image source and configuration parameters to the phpThumb.php file. Few of the basic examples are:

  1. Resize an image to a width of 200px.
 <img border="0" src="../phpThumb.php?src=images/resizeMe.jpg&amp;w=200 " alt="Example 1">
  1. Resize to 200px and heavy blur, set to value 5.
 <img border="0" src="../phpThumb.php?src=images/resizeMe.jpg&amp;w=200&amp;fltr[]=blur|5" alt="Example 2">

Demo and Download

The phpThumb() is really easy to use. All the features are demonstrated on the Project demo page. The library can be downloaded from the product page on Sourceforge.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>