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.
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().
You may not execute several scaling functions in a row without saving in between.
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).
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());
}
?>