I put together a simple, lightweight PHP class for generating thumbnail images. The class is compatible with PHP5 and uses the GD2 extension (included by default with PHP5) to create JPEG, PNG, and GIF thumbnails. I’ve setup a Google Code project for it: Gregphoto_Image where you can checkout the source from SVN and file bugs.
The class has the following basic features:
- Ability to read JPEG, PNG, or GIF images
- Ability to output JPEG, PNG, or GIF images
- 4 modes of thumbnail creation
- MAX_HEIGHT – you specify a maximum height and the dimensions are calculated based off of the height
- MAX_WIDTH – you specify a maximum width and the dimensions are calculated based off of the width
- BEST_FIT – you specify a maximum height and width and the dimensions are calculated so that the thumbnail
is as large as possible without exceeding the maximum height or width - EXACT – you specify a maximum height and width and these are directly used. Causes distortion if the
chosen aspect ratio is different from the aspect ratio of the image
- Renders/saves images in their input format by default, but allows changing the format. For example, input a GIF but output a PNG
- Fully documented object oriented code
- Fluent interface for creating thumbnails with a minimal amount of code
The class is licensed under the MIT license, which basically means it can be used and modified by anyone – for personal or commercial use.
You can Download it from the project page on Google Code. You can view examples of it running on my site – the examples are checked into SVN and can be viewed on the project site. You can also view the docs.
Example usage:
1 2 3 4 5 6 7 | require('path/to/Gregphoto_Image.php'); $image = new Gregphoto_Image('path/to/sample/image.jpg'); $image->setMaxHeight(200); $image->setMaxWidth(200); $image->setJpegQuality(90); $image->resize(Gregphoto_Image::BEST_FIT); $image->showThumbnail(); |
1 2 | $image = new Gregphoto_Image('../images/fan.jpg'); $image->setMaxHeight(200)->setJpegQuality(90)->resize()->showThumbnail(); |
1 2 3 4 5 6 7 8 | require('path/to/Gregphoto_Image.php'); $image = new Gregphoto_Image('path/to/image.jpg'); $image->setMaxHeight(200); $image->setMaxWidth(200); $image->setJpegQuality(90); $image->setOutputType(IMAGETYPE_PNG); $image->resize(Gregphoto_Image::BEST_FIT); $image->saveThumbnail('path/to/thumbnail.png'); |
Enjoy!
January 4th, 2008 at 2:57 am
Excellent tool for working with thumbnails and all types of images.
I wonder if there is anything for PHP4 either?
January 31st, 2008 at 1:27 am
Nice and simple class, good subjects about webdevelopment on your blog.
Keep up the good work Greg.
February 11th, 2008 at 8:24 am
Are you planning to add .png transparency support?
Thanks
Matthew
October 8th, 2008 at 3:17 am
I have a db with image links. Why is the class not working when I call it in a loop to display all the images in the db? Help please.
January 30th, 2009 at 8:50 am
Hi Greg,
Tried it and was extremely happy with the outcome.
Thanks
George
June 14th, 2009 at 9:56 am
Great piece of software.
Thanks
July 5th, 2009 at 6:51 pm
This article was so excellent. it is good for me to read.
July 6th, 2009 at 6:48 pm
you can buy what you want here
August 4th, 2009 at 6:12 pm
Great class geg! I’m kinda new at this because I outsource my technical stuff to people I hire for $3 an hour to do it.
I learned to outsource from a great course from http://www.virtualadministrativeassistant.info/minicourse1/index.html
Thanks for the great info I’m going to have my programmer look at your post.
January 4th, 2010 at 8:12 am
Black GHD
March 12th, 2010 at 2:49 am
Other brands zoom kobe basketball and dragon bird, his Nike Airmax 360, stone, a star, fire, maple,lady nike shoes, Wallace, send lotte basically is wholesale distribution or local market sale, there have been basketball shoes sales scale, and brand influence more mentioned, most people know non-professional brand such as its existence.http://www.airmax-online.com/
March 12th, 2010 at 2:50 am
In order to adapt to the Nike Shocks for women increasingly fierce competition. Online Store http://www.nikedirect.net/
March 12th, 2010 at 2:51 am
The nike air max from Nike is one of the most legendary basketball shoes, probably with the highest degree of recognition.
March 12th, 2010 at 2:52 am
All this shox nike seems and mass consumers to go. It is natural to attract most consumers not to go hiking,basketball shoes also won’t go adventure, usually also little contact with bad outdoor environment.
March 12th, 2010 at 2:53 am
Thus helping out design idea in the instant action athletes to speed, strength Nike Dunk SB Shoes and coordination of dexterity.Multi-functional high-performance products based on the concept of" source nike 7.0 shoes .