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:
- Image can be watermarked by another image or text.
- Border or frame can be added to the output image.
- Control contrast and brightness levels.
- Blur images and drop shadows.
- Rounded Corners
- Flip image either horizontally or vertically
Few other filters are also available.
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:
- Resize an image to a width of 200px.
<img border="0" src="../phpThumb.php?src=images/resizeMe.jpg&w=200 " alt="Example 1">
- Resize to 200px and heavy blur, set to value 5.
<img border="0" src="../phpThumb.php?src=images/resizeMe.jpg&w=200&fltr=blur|5" alt="Example 2">