Image_Transform

Image_Transform main use case is to create thumbnails of images.

General usage

The first step to do when using Image_Transform is to create an Image_Transform_Driver instance via the static factory() method. Just pass the driver name and you've got your object.

Note : You may omit the driver name. In that case, Image_Transform checks for Imagick2, GD and Imlib and uses whatever driver is found first.

Now you can load() your image by passing the filename to this function. Use one of the scaling methods and then save().

Avertissement

You may not execute several scaling functions in a row without saving in between.

Exemple 49-2. Scaling an image down


<?php
require_once 'Image/Transform.php';

//create transform driver object
$it Image_Transform::factory('GD');

//load the original file
$it->load('beach-large.jpg');

//scale it to 150px
$it->scaleMaxLength(150);

//save it into a different file
$it->save('beach-150px.jpg');
?>

The example above did not do any error checking. Principially, any method may return a PEAR_Error object. Either you check each return value or - in PHP 5 - set the global PEAR error handler to throw exceptions as soon as an error occurs. This may interfere with other errors that are expected and can be hidden, so be careful with this option (especially when using other packages).

Exemple 49-3. Scaling an image and checking for all possible errors


<?php
require_once 'Image/Transform.php';

//create transform driver object
$it Image_Transform::factory('GD');
if (PEAR::isError($it)) {
    die($it->getMessage());
}

//load the original file
$ret $it->load('beach-large.jpg');
if (PEAR::isError($ret)) {
    die($ret->getMessage());
}

//scale it to 150px
$ret $it->scaleByLength(150);
if (PEAR::isError($ret)) {
    die($ret->getMessage());
}

//save it into a different file
$ret $it->save('beach-150px.jpg');
if (PEAR::isError($ret)) {
    die($ret->getMessage());
}
?>

Drivers

The Image_Transform package is useless without a driver that encapsulates some graphic libraries' methods. As of April 2008, the package has the following drivers you can install and use:

Scaling images

Image_Transform brings you a lot of methods to scale images. Most of them call the basic resizing method with different parameters, but still have their right to exist because they make your life convenient. Here is a short list:

Saving

The save() method requires at least one parameter, the filename as which the scaled image is to be saved as. With only one parameter, the type of the new image is the same as the original type.

Note : File extensions are not appended if left out.

The second parameter can be the extension of the file type you want to save the image as, for example png or jpg.

In case of an error - for example if the driver does not support to write the file type - a PEAR_Error object is returned.

Instead of saving, you can directly put out the image to the browser using display().

Other methods

Tableau 49-3. Driver support for transformation methods

MethodGDImagick2Imagick3ImlibIMNetPBM
_resize()yesyesyesyesyesyes
save()yesyesyesyesyesyes
display()yesyesyesyesyesyes
free()yesyesyesyesyesyes
addText()yesyesyesyesyesyes
addDropShadow()------
addBorder()yes-----
crop()yesyesyesyesyesyes
canvasResize()------
fitOnCanvas()------
flip()yesyesyesyesyesyes
gamma()yesyesyes-yesyes
greyscale()yes-yes-yesyes
mirror()yesyesyesyesyesyes
normalize()------
rotate()yesyesyesyesyesyes