cvs: pear /Image_Transform Image_Transform.php Image_Transform_GD.php Image_Transform_IM.php Image_Transform_NetPBM.php

From: Date: Thu, 16 May 2002 18:08:26 +0000
Subject: cvs: pear /Image_Transform Image_Transform.php Image_Transform_GD.php Image_Transform_IM.php Image_Transform_NetPBM.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-3866@lists.php.net to get a copy of this message
reywob Thu May 16 14:08:26 2002 EDT Added files: /pear/Image_Transform Image_Transform.php Image_Transform_GD.php Image_Transform_IM.php Image_Transform_NetPBM.php Log: First commit. The actual PHP files that somehow didn't go last time :(

Index: pear/Image_Transform/Image_Transform.php +++ pear/Image_Transform/Image_Transform.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | // | Alan Knowles <alan@akbkhome.com> | // | Vincent Oostindie <vincent@sunlight.tmfweb.nl> | // +----------------------------------------------------------------------+ // // $Id: Image_Transform.php,v 1.1 2002/05/16 18:08:26 reywob Exp $ // // Image Transformation interface // # TODO # Make scaleMaxX and scaleMaxY accept all the various dimensions (factor % etc) require_once "PEAR.php"; /* if(!defined(IMAGE_TRANSFORM_LIB_PATH)){ define('IMAGE_TRANSFORM_LIB_PATH', '/usr/local/ImageMagick/bin/'); }*/ /** * The main "Image_Resize" class is a container and base class which * provides the static methods for creating Image objects as well as * some utility functions (maths) common to all parts of Image Resize. * * The object model of DB is as follows (indentation means inheritance): * * Image_Resize The base for each Image implementation. Provides default * | implementations (in OO lingo virtual methods) for * | the actual Image implementations as well as a bunch of * | maths methods. * | * +-Image_GD The Image implementation for the PHP GD extension . Inherits * Image_Resize * When calling DB::setup for GD images the object returned is an * instance of this class. * * @package Image Resize * @version 1.00 * @author Peter Bowyer <peter@mapledesign.co.uk> * @since PHP 4.0 */ Class Image_Transform { /** * Name of the image file * @var string */ var $image = ''; /** * Type of the image file (eg. jpg, gif png ...) * @var string */ var $type = ''; /** * Original image width in x direction * @var int */ var $img_x = ''; /** * Original image width in y direction * @var int */ var $img_y = ''; /** * New image width in x direction * @var int */ var $new_x = ''; /** * New image width in y direction * @var int */ var $new_y = ''; /** * Path the the library used * e.g. /usr/local/ImageMagick/bin/ or * /usr/local/netpbm/ */ var $lib_path = ''; /** * Flag to warn if image has been resized more than once before displaying * or saving. */ var $resized = false; /** * Create a new Image_resize object * * @param mixed $options An associative array of option names and * their values. See Image_Resize::setOption for more information on * setup options. * * @return mixed a newly created Image_Resize object, or a PEAR * error object on error * * @see PEAR::isError() * @see Image_Resize::setOption() */ function setup($image_lib) { include_once "Image_Transform/Image_Transform_$image_lib.php" ; $classname = "Image_Transform_${image_lib}"; $obj =& new $classname; return $obj; } // End setup /** * Resize the Image in the X and/or Y direction * If either is 0 it will be scaled proportionally * * @access public * * @param mixed $new_x (0, number, percentage 10% or 0.1) * @param mixed $new_y (0, number, percentage 10% or 0.1) * * @return mixed none or PEAR_error */ function resize($new_x = 0, $new_y = 0) { // 0 means keep original size if (0 == $new_x) { $new_x = $this->img_x; } if (0 == $new_y) { $new_y = $this->img_y; } // Record the new size of the image $this->new_x = $this->_parse_size($new_x, $this->img_x); $this->new_y = $this->_parse_size($new_y, $this->img_y); // Now do the library specific resizing. $this->_resize($this->new_x, $this->new_y); } // End resize /** * Scale the image to have the max x dimension specified. * * @param int $new_x Size to scale X-dimension to * @return none */ function scaleMaxX($new_x) { $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); $this->_resize($new_x, $new_y); } // End resizeX /** * Scale the image to have the max y dimension specified. * * @access public * @param int $new_y Size to scale Y-dimension to * @return none */ function scaleMaxY($new_y) { $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); $this->_resize($new_x, $new_y); } // End resizeY /** * Scale Image to a maximum or percentage * * @access public * @param mixed (number, percentage 10% or 0.1) * @return mixed none or PEAR_error */ function scale($size) { $strlen = strlen($size); if ($strlen > 0 && '%' == $size{-1}) { $this->scaleByPercentage(substr($size, 0, -1)); } elseif ($size < 1) { $this->scaleByFactor($size); } else { $this->scaleByLength($size); } } // End scale /* The old scale function! function scale($size) { if ($this->img_x >= $this->img_y) { $new_x = _parse_size($size, $this->img_x); $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); } else { $new_y = _parse_size($size, $this->img_y); $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); } $this->_resize($new_x, $new_y); } // End scale */ /** * 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 * * @access public * @param int $size Percentage of original size to scale to * @return none */ function scaleByPercentage($size) { $this->scaleByFactor($size / 100); } // 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. * * @access public * @param float $size Factor of original size to scale to * @return none */ function scaleByFactor($size) { $new_x = round($size * $this->img_x, 0); $new_y = round($size * $this->img_y, 0); $this->_resize($new_x, $new_y); } // End scaleByFactor /** * Scales an image so that the longest side has this dimension. * * @access public * @param int $size Max dimension in pixels * @return none */ function scaleByLength($size) { if ($this->img_x >= $this->img_y) { $new_x = $size; $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); } else { $new_y = $size; $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); } $this->_resize($new_x, $new_y); } // End scaleByLength /** * Parse input and convert * If either is 0 it will be scaled proportionally * * @access private * * @param mixed $new_size (0, number, percentage 10% or 0.1) * @param int $old_size * * @return mixed none or PEAR_error */ function _parse_size($new_size, $old_size) { if ('%' == $new_size) { $new_size = str_replace('%','',$new_size); $new_size = $new_size / 100; } if ($new_size > 1) { return (int) $new_size; } elseif ($new_size == 0) { return (int) $old_size; } else { return (int) round($new_size * $old_size, 0); } } /** * Place holder for the real resize method * used by extended methods to do the resizing * * @access private * @return PEAR_error */ function _resize() { return PEAR::raiseError("No Resize method exists", true); } /** * Place holder for the real load method * used by extended methods to do the resizing * * @access public * @return PEAR_error */ function load($filename) { return PEAR::raiseError("No Load method exists", true); } /** * Place holder for the real display method * used by extended methods to do the resizing * * @access public * @param string filename * @return PEAR_error */ function display($type, $quality) { return PEAR::raiseError("No Display method exists", true); } /** * Place holder for the real save method * used by extended methods to do the resizing * * @access public * @param string filename * @return PEAR_error */ function save($filename) { return PEAR::raiseError("No Save method exists", true); } function addText() { } function addDropShadow() { } function addBorder() { } function crop() { } } ?> Index: pear/Image_Transform/Image_Transform_GD.php +++ pear/Image_Transform/Image_Transform_GD.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | // | Alan Knowles <alan@akbkhome.com> | // +----------------------------------------------------------------------+ // // $Id: Image_Transform_GD.php,v 1.1 2002/05/16 18:08:26 reywob Exp $ // // Image Transformation interface using the GD library // require_once "Image_Transform/Image_Transform.php"; Class Image_Transform_GD extends Image_Transform { /** * Holds the image file for manipulation */ var $imageHandle = ''; /** * Holds the original image file */ var $old_image = ''; /** * Check settings * * @return mixed true or or a PEAR error object on error * * @see PEAR::isError() */ function Image_Transform_GD() { if (!function_exists("ImageTypes")) return PEAR::raiseError("libgd not compiled into PHP", true); if (!ImageTypes()) return PEAR::raiseError("No supported image types available", true); return; } // End function Image /** * Load image * * @param string filename * * @return mixed none or a PEAR error object on error * @see PEAR::isError() */ function load($image) { $this->image = $image; $this->type = $this->_get_type(); $functionName = 'ImageCreateFrom' . $this->type; $this->imageHandle = $functionName($this->image); $this->_get_size(); } // End load function addText($params) { $default_params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => 'red', 'font' => 'Arial.ttf', 'size' => '12', 'angle' => 0; 'resize_first' => false // Carry out the scaling of the image before annotation? ); $params = array_merge($default_params, $params); extract($params); if ('ttf' == substr($font, -3)) { ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); } else { ImagePSText($this->imageHandle, $size, $angle, $x, $y, $color, $font, $text); } } // End addText /* function resize($new_x, $new_y = '') { if ($new_y == '') { $new_y = ($new_x / $this->img_x) * $this->img_y; } $new_img = ImageCreateTrueColor($new_x,$new_y); ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); $this->old_image = $this->imageHandle; $this->new_x = $new_x; $this->new_y = $new_y; $this->imageHandle = $new_img; } // End resize function scale($size) { if ($this->img_x >= $this->img_y) { $new_x = $size; $new_y = ($new_x / $this->img_x) * $this->img_y; } else { $new_y = $size; $new_x = ($new_y / $this->img_y) * $this->img_x; } $this->_resize($new_x, $new_y); } // End scale */ /** * Resize Action * * @return none * @see PEAR::isError() */ function _resize($new_x, $new_y) { if ($this->resized === true) { PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE); } $new_img = ImageCreateTrueColor($new_x,$new_y); ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); $this->old_image = $this->imageHandle; $this->imageHandle = $new_img; $this->resized = true; } /** * Save the image file * * @param $filename string the name of the file to write to * @return none */ function save($filename) { $functionName = 'Image' . $this->type; $functionName($this->imageHandle, $filename) ; $this->imageHandle = $this->old_image; $this->resized = false; } // End save /** * Display image without saving and lose changes * * @param string type (JPG,PNG...); * @param int quality 75 * * @return none */ function display($type = '', $quality = 75) { if ($type != '') { $this->type = $type; } $functionName = 'Image' . $this->type; header('Content-type: image/' . strtolower($this->type)); $functionName($this->imageHandle, '', $quality); $this->imageHandle = $this->old_image; $this->resized = false; } /** * Destroy image handle * * @return none */ function destroy() { if ($this->imageHandle){ ImageDestroy($this->imageHandle); } } /** * get the image type * * @return string (gif,jpeg,png) */ function _get_type () { // Can I pass $image by reference? $data = GetImageSize($this->image); switch($data[2]){ case '1': $type = 'gif'; break; case '2': $type = 'jpeg'; break; case '3': $type = 'png'; break; } return $type; } /** * get the image size (into img_x and img_y) * * @return none */ function _get_size() { $size = GetImageSize($this->image); $this->img_x = $size[0]; $this->img_y = $size[1]; } // End _get_size } // End class ImageIM ?> Index: pear/Image_Transform/Image_Transform_IM.php +++ pear/Image_Transform/Image_Transform_IM.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | // +----------------------------------------------------------------------+ // // $Id: Image_Transform_IM.php,v 1.1 2002/05/16 18:08:26 reywob Exp $ // // Image Transformation interface using command line ImageMagick // require_once "Image_Transform/Image_Transform.php"; Class Image_Transform_IM extends Image_Transform { /** * associative array commands to be executed * @var array */ var $command = array(); /** * * */ function Image_Transform_IM() { return true; } // End function Image_IM /** * Load image * * @param string filename * * @return mixed none or a PEAR error object on error * @see PEAR::isError() */ function load($image) { if (!file_exists($image)) { return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true); } $this->image = $image; $this->_get_size(); // Should the image type be lower or upper case? exec($this->lib_path . 'identify -format "%m" ' . $image, $data); $this->type = strtolower($data[0]); } // End load /** * Resize Action * * @return none * @see PEAR::isError() */ function _resize($new_x, $new_y) { if (isset($this->command['resize'])) { return PEAR::raiseError("You cannot scale or resize an image more than once without calling save or display", true); } $this->command['resize'] = "-geometry ${new_x}x${new_y}!"; } // End resize /* function resize($new_x, $new_y = '') { if ($new_y == '') { $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); } // Record the new size of the image $this->new_x = $new_x; $this->new_y = $new_y; $this->command = "convert -geometry ${new_x}x${new_y}!"; } // End resize function scale($size) { if ($this->img_x >= $this->img_y) { $new_x = $size; $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); } else { $new_y = $size; $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); } $this->resize($new_x, $new_y); } // End scale */ /** * rotate * */ function rotate($angle) { $this->command['rotate'] = "-rotate $angle"; } // End rotate /** * addText * */ function addText($params) { /* $params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => 'red', 'font' => 'Arial', 'resize_first' => false ); */ $default_params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => 'red', 'font' => 'Arial.ttf', 'resize_first' => false // Carry out the scaling of the image before annotation? ); $params = array_merge($default_params, $params); extract($params); if (true === $resize_first) { // Set the key so that this will be the last item in the array $key = 'ztext'; } else { $key = 'text'; } $this->command[$key] = "-font $font -fill $color -draw 'text $x,$y \"$text\"'"; // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory]. } // End addText /** * Save the image file * * @param $filename string the name of the file to write to * @return none */ function save($filename, $quality = 75) { $cmd = 'unlimit;' . $this->lib_path . 'convert ' . implode(' ', $this->command) . " -quality $quality " . $this->image . ' ' . $filename . ' 2>&1'; passthru($cmd); print $cmd; } // End save /** * Display image without saving and lose changes * * @param string type (JPG,PNG...); * @param int quality 75 * * @return none */ function display($type = '', $quality = 75) { if ($type == '') { header('Content-type: image/' . $this->type); passthru($this->lib_path . 'convert ' . implode(' ', $this->command) . " -quality $quality " . $this->image . ' ' . strtoupper($this->type) . ":-"); } else { header('Content-type: image/' . $type); passthru($this->lib_path . 'convert ' . implode(' ', $this->command) . " -quality $quality " . $this->image . ' ' . strtoupper($type) . ":-"); } } /** * Destroy image handle * * @return none */ function destroy() { return true; } /** * get the image size (into img_x and img_y) * * @return none */ function _get_size() { $str = exec($this->lib_path . "identify -format \"%w::%h\" " . $this->image, $data); $str = split('::', $data[0]); $this->img_x = $str[0]; $this->img_y = $str[1]; } // End _get_size } // End class ImageIM ?> Index: pear/Image_Transform/Image_Transform_NetPBM.php +++ pear/Image_Transform/Image_Transform_NetPBM.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | // +----------------------------------------------------------------------+ // // $Id: Image_Transform_NetPBM.php,v 1.1 2002/05/16 18:08:26 reywob Exp $ // // Image Transformation interface using command line NetPBM // require_once "Image_Transform/Image_Transform.php"; Class Image_Transform_NetPBM extends Image_Transform { /** * associative array commands to be executed * @var array */ var $command = array(); /** * * */ function Image_Transform_NetPBM() { return true; } // End function Image_NetPBM /** * Load image * * @param string filename * * @return mixed none or a PEAR error object on error * @see PEAR::isError() */ function load($image) { $this->image = $image; $this->type = $this->_get_type(); $this->_get_size(); } // End load /** * Resize Action * * @return none * @see PEAR::isError() */ function _resize($new_x, $new_y) { if (isset($this->command['resize'])) { return PEAR::raiseError("You cannot scale or resize an image more than once without calling save or display", true); } $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmscale -width $new_x -height $new_y"; } // End resize /** * rotate * * @since * @param int $angle The angle to rotate the image through */ function rotate($angle) { if ('-' == $angle{0}) { $angle = 360 - substr($angle, 1); } $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmflip -r$angle"; } // End rotate /** * addText * */ function addText($params) { $default_params = array( 'text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => 'red', 'font' => 'Arial.ttf', 'size' => '12', 'angle' => 0; 'resize_first' => false // Carry out the scaling of the image before annotation? ); $params = array_merge($default_params, $params); extract($params); $this->command[] = "ppmlabel -angle $angle -colour $color -size $size -text $text -x $x -y $y"; } // End addText /** * Save the image file * * @param $filename string the name of the file to write to * @return none */ function save($filename, $quality = 75) { $cmd = IMAGE_TRANSFORM_LIB_PATH . $this->type . 'topnm ' . $this->image . '|' . implode('|', $this->command) . '|'; switch($this->type){ case 'jpeg': $arg = "--quality=$quality"; break; case 'gif': $cmd .= IMAGE_TRANSFORM_LIB_PATH . "ppmquant 256|"; break; default: break; } // switch $cmd .= IMAGE_TRANSFORM_LIB_PATH . 'ppmto' . $this->type . ' ' . $args . ' > ' . $filename . ' 2>&1'; passthru($cmd); } // End save /** * Display image without saving and lose changes * * @param string $type (JPG,PNG...); * @param int $quality 75 * * @return none */ function display($type = '', $quality = 75) { if ($type == '') { $type = $this->type; } header('Content-type: image/' . $type); $cmd = IMAGE_TRANSFORM_LIB_PATH . $type . 'topnm ' . $this->image . '|' . implode('|', $this->command) . '|'; switch($type){ case 'jpeg': $arg = "--quality=$quality"; break; case 'gif': $cmd .= IMAGE_TRANSFORM_LIB_PATH . "ppmquant 256|"; break; default: break; } // switch $cmd .= IMAGE_TRANSFORM_LIB_PATH . 'ppmto' . $type . ' ' . $arg . ' 2>&1'; passthru($cmd); } } /** * Destroy image handle * * @return none */ function destroy() { return true; } /** * get the image size (into img_x and img_y) * * @return none */ function _get_size() { $size = GetImageSize($this->image); $this->img_x = $size[0]; $this->img_y = $size[1]; } // End _get_size /** * get the image type * * @return string (gif,jpeg,png) */ function _get_type () { // Can I pass $image by reference? $data = GetImageSize($this->image); switch($data[2]){ case '1': $type = 'gif'; break; case '2': $type = 'jpeg'; break; case '3': $type = 'png'; break; } return $type; } } // End class ImageIM ?>
« previous php.pear.cvs (#3866) next »