PHP Thumbnailer - GIF, JPG, and PNG Support
Now with cropping!
I've compiled code from many other sources to put together an easy-to-use way to quickly create and present thumbnails of gif, jpg, and png files based on height or width restrictions and with controls for setting the output quality. The function also includes a cache control to prevent the thumbnails from being created more than once as well as the capability to square crop images which is great for galleries/grids.
Demo / Test Sheet:
View the demo/test sheet to view examples of different uses of the code.
Implementation:
Using the function is done by simply including the function in your code or in an include file, then calling the function with the variables you wish. This can be used in a loop for displaying a library, or simply for a sinlge image.
There are 5 variables which are passed to the function:
- The path of the file (relative to root)
- The type of restriction ('width' or 'height')
- The size of the restriction to be placed on the file (in px)
- The quality of the output image (1-10)
- Square crop the image (true/false)
buildThumbnail('/website/images/somefile.jpg','width',200,5,false); IMPORTANT NOTE: Thumbnails will be created in a "thumbs" directory inside the same directory as the file you are transforming. Make sure to add that folder, and give 'write' permissions to this folder. Files will be preppended with 'thm_' so they can be easily identified.
Download
Download the PHP Thumbnailer file (just remove the .txt extension).
Responses to this Article:
Loading Comments...
I am a web developer, designer, and consultant located in the La Crosse / Onalaska Wisconsin region with
over twelve years experience developing and managing projects ranging from large applications and cloud-based
business solutions to social/new media campaigns, to complete system and infrastructure implementation.