ImageSizer class

creating thumbnails on the fly

I created this class to generate thumbnails for images using PHP. Although other scripts exist (phpThumb comes to mind), they're either not flexible enough or too complicated. This is a simple class in one file that can easily be incorporated into any application.

Note: This class uses php's image functions, which requires the GD library.

Usage

object ImageSizer(string filename, string path, 
                  int width, int height, float percentage) 
 
<?php
/* 
 * create a thumbnail file in images/pics/thumbs/ dir 
 * (dir created if it doesn't exist) 
 * setting the width
 */
$image = new ImageSizer('pic.gif', 'images/', 75);  
$thumbnail = $image->getThumbnail();  //'images/thumbs/th_pic.gif'
 
/* 
 * set the height, automatically resizing the width
 */
$image = new ImageSizer('pic.gif', 'images/', 'auto', 150);
$thumbnail = $image->getThumbnail();  //'images/thumbs/th_pic.gif'
 
/*
 * size by percentage (75%)
 */
$image = new ImageSizer('pic.gif', 'images/', 'auto', 'auto', 0.75);
$thumbnail = $image->getThumbnail();  //'images/thumbs/th_pic.gif'
 
/*
 * create a sub directory under thumbs
 */
$image = new ImageSizer('pic.gif', 'images/', 75);  
$image->setSubDir('mythumbs'); 
// 'images/thumbs/mythumbs/th_pic.gif'
$thumbnail = $image->getThumbnail();  
 
/*
 * place thumbs in user-defined directory 
 * (instead of the default 'thumbs')
 */
$image = new ImageSizer('pic.gif', 'images/', 75);  
$image->setOutputDir('thmbdir');
$thumbnail = $image->getThumbnail(); //'images/thumbdir/th_pic.gif'
?>

Documentation

phpWinDoc-phpDocumentor generated doc and source can be found here.

Download

The following zip file contains the ImageSizer class. To install, save the file and extract ImageSizer.php to a directory in your include path and use.
ImageSizer.zip
BACK TO TOP