Source for file Transform.php
Documentation is available at Transform.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* Simple and cross-library package to doing image transformations and
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @package Image_Transform
* @author Vincent Oostindie <vincent@sunlight.tmfweb.nl>
* @author Alan Knowles <alan@akbkhome.com>
* @author Peter Bowyer <peter@mapledesign.co.uk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 2002-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Transform.php,v 1.42 2007/04/19 16:17:57 dufuz Exp $
* @link http://pear.php.net/package/Image_Transform
* Include for error handling
* Error code for unsupported library, image format or methods
define('IMAGE_TRANSFORM_ERROR_UNSUPPORTED', 1 );
* Error code for failed transformation operations
define('IMAGE_TRANSFORM_ERROR_FAILED', 2 );
* Error code for failed i/o (Input/Output) operations
define('IMAGE_TRANSFORM_ERROR_IO', 3 );
* Error code for invalid arguments
define('IMAGE_TRANSFORM_ERROR_ARGUMENT', 4 );
* Error code for out-of-bound related errors
define('IMAGE_TRANSFORM_ERROR_OUTOFBOUND', 5 );
* Base class with factory method for backend driver
* The main "Image_Transform" class is a container and base class which
* provides a static method for creating an Image object as well as
* some utility functions (maths) common to all parts of Image_Transform.
* @package Image_Transform
* @author Alan Knowles <alan@akbkhome.com>
* @author Peter Bowyer <peter@mapledesign.co.uk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 2002-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: @package_version@
* @link http://pear.php.net/package/Image_Transform
* Type of the image file (eg. jpg, gif png ...)
* Original image width in x direction
* Original image width in y direction
* New image width in x direction
* New image width in y direction
* Path to the library used
* e.g. /usr/local/ImageMagick/bin/ or
* Flag to warn if image has been resized more than once before displaying
* @var array General options
'scaleMethod' => 'smooth',
'canvasColor' => array (255 , 255 , 255 ),
'pencilColor' => array (0 , 0 , 0 ),
'textColor' => array (0 , 0 , 0 )
* Flag for whether settings should be discarded on saving/display of image
* @see Image_Transform::keepSettingsOnSave
* Initialization error tracking
* associative array that tracks existence of programs
* (for drivers using shell interface and a tiny performance
* improvement if the clearstatcache() is used)
* Default parameters used in the addText methods.
'resize_first' => false );
* Creates a new Image_Transform object
* @param string $driver name of driver class to initialize. If no driver
* is specified the factory will attempt to use 'Imagick' first
* then 'GD' second, then 'Imlib' last
* @return object an Image_Transform object, or PEAR_Error on error
* @see Image_Transform::setOption()
foreach ($extensions as $ext => $ext_driver) {
if (PEAR ::loadExtension ($ext)) {
return PEAR ::raiseError ('No image library specified and none can be found. You must specify driver in factory() call.',
@include_once 'Image/Transform/Driver/' . basename($driver) . '.php';
$classname = " Image_Transform_Driver_{$driver}";
return PEAR ::raiseError ('Image library not supported... aborting.',
if ($error = & $obj->isError ()) {
* Returns/sets an error when the instance couldn't initialize properly
* @param object PEAR_Error object when setting an error
* @return mixed FALSE or PEAR_Error object
* Resizes the image in the X and/or Y direction(s)
* If either is 0 it will keep the original size for that dimension
* @param mixed $new_x (0, number, percentage 10% or 0.1)
* @param mixed $new_y (0, number, percentage 10% or 0.1)
* @param array $options Options
* @return mixed TRUE or PEAR_Error object on error
function resize($new_x = 0 , $new_y = 0 , $options = null )
// 0 means keep original size
// Now do the library specific resizing.
return $this->_resize($new_x, $new_y, $options);
* Scales the image to the specified width
* This method preserves the aspect ratio
* @param int $new_x Size to scale X-dimension to
* @return mixed TRUE or PEAR_Error object on error
return PEAR ::raiseError ('New size must be strictly positive',
function scaleByXY($new_x = 0 , $new_y = 0 , $options = null )
return $this->resize($new_x, $new_y, $options);
* Scales the image to the specified height.
* This method preserves the aspect ratio
* @param int $new_y Size to scale Y-dimension to
* @return mixed TRUE or PEAR_Error object on error
return PEAR ::raiseError ('New size must be strictly positive',
* Scales an image by a percentage, factor or a given length
* This method preserves the aspect ratio
* @param mixed (number, percentage 10% or 0.1)
* @return mixed TRUE or PEAR_Error object on error
* @see scaleByPercentage, scaleByFactor, scaleByLength
* Scales an image to a percentage of its original size. For example, if
* my image was 640x480 and I called scaleByPercentage(10) then the image
* would be resized to 64x48
* @param int $size Percentage of original size to scale to
* @return mixed TRUE or PEAR_Error object on error
} // End scaleByPercentage
* Scales an image to a factor of its original size. For example, if
* my image was 640x480 and I called scaleByFactor(0.5) then the image
* would be resized to 320x240.
* @param float $size Factor of original size to scale to
* @return mixed TRUE or PEAR_Error object on error
return PEAR ::raiseError ('New size must be strictly positive',
* Scales an image so that the longest side has the specified dimension.
* This method preserves the aspect ratio
* @param int $size Max dimension in pixels
* @return mixed TRUE or PEAR_Error object on error
return PEAR ::raiseError ('New size must be strictly positive',
* Alias for scaleMaxLength
* @return mixed TRUE or PEAR_Error object on error
* Fits the image in the specified box size
* If the image is bigger than the box specified by $width and $height,
* it will be scaled down to fit inside of it.
* If the image is smaller, nothing is done.
* @return bool|PEAR_ErrorTRUE or PEAR_Error object on error
function fit($width, $height)
if ($width <= 0 || $height <= 0 ) {
return PEAR ::raiseError ("Invalid arguments.",
$x = $this->img_x / $width;
$y = $this->img_y / $height;
if ($x <= 1 && $y <= 1 ) {
* Fits the image in the specified width
* If the image is wider than the width specified by $width,
* it will be scaled down to fit inside of it.
* If the image is smaller, nothing is done.
* @return bool|PEAR_ErrorTRUE or PEAR_Error object on error
return ($this->img_x <= $width) ? true : $this->scaleByX($width);
* Fits the image in the specified height
* If the image is taller than the height specified by $height,
* it will be scaled down to fit inside of it.
* If the image is smaller, nothing is done.
* @return bool|PEAR_ErrorTRUE or PEAR_Error object on error
return ($this->img_y <= $height) ? true : $this->scaleByY($height);
* @param string Name of option
* @param mixed Value of option
* Sets multiple options at once
* Associative array of options:
* - quality (Integer: 0: poor - 100: best)
* - scaleMethod ('smooth', 'pixel')
* @param array $options Array of options
* Sets the image type (in lowercase letters), the image height and width.
* @return mixed TRUE or PEAR_error
* @see PHP_Compat::image_type_to_mime_type()
* @link http://php.net/getimagesize
// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
// 7 = TIFF (intel byte order), 8 = TIFF (motorola byte order),
// 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF,
return PEAR ::raiseError ("Cannot fetch image or images details.", true );
return PEAR ::raiseError ("Cannot recognize image format",
* Returns the matching IMAGETYPE_* constant for a given image type
* @param mixed $type String (GIF, JPG,...)
* @return mixed string or integer or input on error
* @see PHP_Compat::image_type_to_mime_type()
return (isset ($types[$t = strtolower($type)])) ? $types[$t] : $type;
* Parses input for number format and convert
|