Source for file GD.php
Documentation is available at GD.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* GD implementation for Image_Transform package
* 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
* @subpackage Image_Transform_Driver_GD
* @author Alan Knowles <alan@akbkhome.com>
* @author Peter Bowyer <peter@mapledesign.co.uk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 2002-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: GD.php,v 1.32 2007/04/19 16:36:09 dufuz Exp $
* @link http://pear.php.net/package/Image_Transform
require_once 'Image/Transform.php';
* GD implementation for Image_Transform package
* $img =& Image_Transform::factory('GD');
* $img->load('magick.png');
* if ($img->rotate($angle, array(
* 'color_mask' => array(255, 0, 0)))) {
* 'text' => 'Rotation ' . $angle,
* 'font' => '/usr/share/fonts/default/TrueType/cogb____.ttf'));
* @package Image_Transform
* @subpackage Image_Transform_Driver_GD
* @author Alan Knowles <alan@akbkhome.com>
* @author Peter Bowyer <peter@mapledesign.co.uk>
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @copyright 2002-2005 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
* Holds the image resource for manipulation
* @var resource $imageHandle
* Holds the original image file
* @var resource $imageHandle
if (!PEAR ::loadExtension ('gd')) {
$this->isError(PEAR ::raiseError ("GD library is not available.",
* Loads an image from file
* @param string $image filename
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
if (PEAR ::isError ($result)) {
return PEAR ::raiseError ('Image type not supported for input',
$functionName = 'ImageCreateFrom' . $this->type;
return PEAR ::raiseError ('Error while loading image file.',
* Adds a border of constant width around an image
* @param int $border_width Width of border to add
function addBorder($border_width, $color = '')
$new_img = $this->_createImage($new_x, $new_y, $this->true_color);
$options = array ('pencilColor', $color);
$color = $this->_getColor('pencilColor', $options, array (0 , 0 , 0 ));
* @param array $params Array contains options
* 'text' The string to draw
* 'x' Horizontal position
* 'size' Size of the fonts in pixel
* 'resize_first' Tell if the image has to be resized
* before drawing the text
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
$options = array ('fontColor' => $color);
$color = $this->_getColor('fontColor', $options, array (0 , 0 , 0 ));
if ('ttf' == substr($font, -3 )) {
ImageTTFText ($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
ImagePSText ($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
* Rotates image by the given angle
* Uses a fast rotation algorythm for custom angles
* or lines copy for multiple of 90 degrees
* @param int $angle Rotation angle
* @param array $options array(
* 'canvasColor' => array(r ,g, b), named color or #rrggbb
* @author Pierre-Alain Joye
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
function rotate($angle, $options = null )
if (($angle % 360 ) == 0 ) {
$color_mask = $this->_getColor('canvasColor', $options,
// Multiply by -1 to change the sign, so the image is rotated clockwise
* @return mixed TRUE or PEAR_Error object on error
for ($x = 0; $x < $this->new_x; ++ $x) {
* @return TRUE or PEAR Error object on error
for ($y = 0; $y < $this->new_y; ++ $y) {
/* for very large images we may want to use the following
Needs to find out what is the threshhold
for ($x = 0; $x < $this->new_x; ++$x) {
for ($y1 = 0; $y1 < $this->new_y / 2; ++$y1) {
$y2 = $this->new_y - 1 - $y1;
$color1 = imagecolorat($this->imageHandle, $x, $y1);
$color2 = imagecolorat($this->imageHandle, $x, $y2);
imagesetpixel($this->imageHandle, $x, $y1, $color2);
imagesetpixel($this->imageHandle, $x, $y2, $color1);
* Crops image by size and start coordinates
* @param int width Cropped image width
* @param int height Cropped image height
* @param int x X-coordinate to crop at
* @param int y Y-coordinate to crop at
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
function crop($width, $height, $x = 0 , $y = 0 )
if (!$this->intersects($width, $height, $x, $y)) {
$width = min($width, $this->new_x - $x);
$height = min($height, $this->new_y - $y);
return PEAR ::raiseError ('Failed transformation: crop()',
* Converts the image to greyscale
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
* For GD 2.01+ the new copyresampled function is used
* It uses a bicubic interpolation algorithm to get far
* - scaleMethod: "pixel" or "smooth"
* @param int $new_x New width
* @param int $new_y New height
* @param mixed $options Optional parameters
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error object on error
function _resize($new_x, $new_y, $options = null )
return PEAR ::raiseError ('You have already resized the image without saving it. Your previous resizing will be overwritten', null , PEAR_ERROR_TRIGGER , E_USER_NOTICE );
if ($this->new_x == $new_x && $this->new_y == $new_y) {
$scaleMethod = $this->_getOption('scaleMethod', $options, 'smooth');
// Make sure to get a true color image if doing resampled resizing
// otherwise get the same type of image
$trueColor = ($scaleMethod == 'pixel') ? null : true;
$icr_res = ImageCopyResampled ($new_img, $this->imageHandle, 0 , 0 , 0 , 0 , $new_x, $new_y, $this->img_x, $this->img_y);
* Adjusts the image gamma
* @param float $outputgamma
* @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
function gamma($outputgamma = 1.0 )
if ($outputgamma != 1.0 ) {
ImageGammaCorrect ($this->imageHandle, 1.0 , $outputgamma);
* Helper method to save to a file or output the image
* @param string $filename the name of the file to write to (blank to output)
* @param string $types define the output format, default
* is the current used format
* @param int $quality output DPI, default is 75
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error object on error
function _generate($filename, $type = '', $quality = null )
$options = (is_array($quality)) ? $quality : array ();
$options['quality'] = $quality;
$quality = $this->_getOption('quality', $options, 75 );
return PEAR ::raiseError ('Image type not supported for output',
$action = 'output image';
$action = 'save image to file';
$functionName = 'image' . $type;
$result = $functionName($this->imageHandle, $filename, $quality);
$result = $functionName($this->imageHandle, $filename);
return PEAR ::raiseError ('Couldn\'t ' . $action,
* Displays image without saving and lose changes.
* This method adds the Content-type HTTP header
* @param string $type (JPEG, PNG...);
* @return bool|PEAR_ErrorTRUE or PEAR_Error object on error
function display($type = '', $quality = null )
return $this->_generate('', $type, $quality);;
* Saves the image to a file
* @param string $filename the name of the file to write to
* @param string $type the output format, default
* is the current used format
* @param int $quality default is 75
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error object on error
function save($filename, $type = '', $quality = null )
return PEAR ::raiseError ('Filename missing',
return $this->_generate($filename, $type, $quality);
* Returns a new image for temporary processing
* @param int $width width of the new image
* @param int $height height of the new image
* @param bool $trueColor force which type of image to create
* @return resource a GD image resource
function _createImage($width = -1 , $height = -1 , $trueColor = null )
|