Legacy Releases

If you're looking for older releases (such as v2.0) and some basic documentation for them, this is where you want to be. Please not that I no longer support these releases, but they've been working in the wild for quite some time, so you shouldn't have any issues.

Downloads

You can still download the old releases:

Documentation

Note: these docs are no longer maintained, and are essentially copied from the old project page

Overview

A PHP image manipulation class, aimed at generating thumbnails. It features the ability to resize by width, height, and percentage, create custom crops, or square crops from the center, rotate the image, and create Apple™-style reflections. It also features the ability to perform multiple manipulations per instance (also known as chaining), without the need to save and re-initialize the class with every manipulation.

This class comes in both PHP 4 and 5 flavors, but I highly recommend the PHP 5 version, due to the “__destruct()” function that became available in version 5. This helps ensure that you do not inadvertently create memory leaks by forgetting to call the “destruct()” function explicitly, like you must in the PHP 4 version of this script.

What's New?

After the large amount of feedback that I got with the first version of this class, I did a lot of code refactoring, and simplified the API greatly. I have also added some new features:

  • You can now create custom crops, as well as square crops from the center of the image
  • You can also rotate the image clock-wise or counter clock-wise in 90° increments
  • You can now create those fancy Apple™-style reflections that are all the rage with the cool “web 2.0″ kids!

For the uninitiated, the class still contains it’s old features:

  • Works with gif,jpg, and png images
  • Ability to resize by percentage, width, or height
  • Ability to set quality of jpg images (0-100%)
  • Ability to perform multiple manipulations and saves without re-initializing class, or reloading original image
  • Ability to display manipulated image on the fly, enabling dynamic image generation in your scripts

Usage

To begin, you need to create an instance of the class:

<?php  
include_once('thumbnail.inc.php');  
$thumb = new Thumbnail('path/to/image/file.jpg');  
?>

Now you can begin your manipulations. The individual functions are described in detail below.

IMPORTANT PHP 4 NOTE You must remember to call the destruct() function at then end of your scripts in order to deallocate the memory used for the image manipulation, similar to the way you must always call fclose() when you use fopen(). The PHP 5 class does this automatically, so there is no need to explicitly call this function (it doesn’t exist as a matter of fact!).

The API

resize($maxWidth,$maxHeight)

Resizes the image to be now wider than maxWidth and no higher than maxHeight. Both values are required. Example:

<?php
//resize image to no wider than 250 pixels wide and 250 pixels high  
$thumb->resize(250,250); 
?>

resizePercent($percent)

Resizes the image to a percentage of its original size. Example:

<?php
//reduce the image by 50%  
$thumb->resizePercent(50);  
?>

cropFromCenter($cropSize)

Create a square crop of $cropSize pixels from the center of the image. Example:

<?php
//create a 100x100 pixel crop from the center of an image  
$thumb->cropFromCenter(100);  
?>

crop($startX,$startY,$width,$height)

Crop the image at $startX,$startY with a crop area of $width by $height pixels. Example:

<?php
//create a 100x50 pixel crop from the top left corner of an image  
$thumb->crop(0,0,100,50);  
?>

rotateImage($direction)

Rotates the image 90° either clock-wise (default) or counter clock-wise. Example:

<?php
//rotate the image clock-wise  
$thumb->rotateImage();  //also valid: $thumb->rotateImage('CW');  
//rotate the image counter clock-wise  
$thumb->rotateImage('CCW');  
?>

createReflection($percent,$reflection,$white,$border,$borderColor)

Creates an Apple™-style reflection (it’s more of a web 2.0 thing now, I know…) from an image. This one’s a bit weird to explain, but here goes:

  • $percent - What percentage of the image to create the reflection from
  • $reflection - What percentage of the image height should the reflection height be. i.e. If your image is 100 pixels high, and you set reflection to 40, the reflection would be 40 pixels high.
  • $white - How transparent (using white as the background) the reflection should be, as a percent
  • $border - Whether a border should be drawn around the original image (default is true)
  • $borderColor - The hex value of the color you would like your border to be (default is #a4a4a4)

Here’s an example:

And the code that generated it:

<?php
$thumb->createReflection(40,40,80,true,'#a4a4a4');  
?>

Attachments