THE BASICS
Overview
Gregphoto_Image is a PHP class that aims to simplify the process of generating thumbnail images
in the JPEG, PNG, and GIF formats. Images can either be rendered directly to the screen or saved
to the filesystem.
Prerequisites
- PHP version 5 or greater - the class uses PHP5 specific language features such as
exceptions, destructors, and public/private methods.
- The GD2 PHP extension - the class performs all image manipulation. While there are other
graphics libraries available for PHP, I chose to use GD2 because it ships as a part of PHP5
and works well across platforms
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
- MAX_WIDTH - 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
License
This class is licensed under the
MIT license. Do with it what you will...
Getting it
Gregphoto_Image is available on Google Code in the
Gregphoto_Image Project
SAMPLE USAGE
Rendering thumbnails to the browser
In this example, a file is loaded from the file system and is resized to a maximum height of 100 and a maximum
width of 150 with a quality of 90 (out of 100_. It is then rendered to the browser:
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();
Saving thumbnails to the filesystem and setting the output file type
The following shows the same manipulation, but instead it is saved to a file and it is output in a different image format.
The output type can be one of the following three constants: IMAGETYPE_JPEG, IMAGETYPE_GIF, or IMAGETYPE_PNG
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');
More Examples
See the 'samples' directory of the distribution for more examples including:
- Examples of all 4 resizing types
- Examples of outputing images to the browser
- Example of a simple gallery that dynamically shows thumbnails and a main image
- Example of a simple gallery that caches images to the filesystem the first time they are requested
- Example of using the fluent interface