cvs: pear /Image_Transform/Drivers GD.php IM.php NetPBM.php

From: Date: Fri, 31 May 2002 18:05:56 +0000
Subject: cvs: pear /Image_Transform/Drivers GD.php IM.php NetPBM.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4322@lists.php.net to get a copy of this message
reywob Fri May 31 14:05:56 2002 EDT Added files: /pear/Image_Transform/Drivers GD.php IM.php NetPBM.php Log: Driver classes moved to new location

Index: pear/Image_Transform/Drivers/GD.php +++ pear/Image_Transform/Drivers/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: GD.php,v 1.1 2002/05/31 18:05:56 reywob Exp $ // // Image Transformation interface using the GD library // require_once "Image/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 /** * 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; $this->new_x = $new_x; $this->new_y = $new_y; } /** * 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/Drivers/IM.php +++ pear/Image_Transform/Drivers/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: IM.php,v 1.1 2002/05/31 18:05:56 reywob Exp $ // // Image Transformation interface using command line ImageMagick // require_once "Image/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() { include_once 'System/Command.php'; if (!defined('IMAGE_TRANSFORM_LIB_PATH')) { define('IMAGE_TRANSFORM_LIB_PATH', escapeshellcmd(System_Command::which('convert')) . '/'); } 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(IMAGE_TRANSFORM_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}!"; $this->new_x = $new_x; $this->new_y = $new_y; } // End resize /** * 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 = 'ulimit;' . IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . escapeshellarg($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(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . strtoupper($this->type) . ":-"); } else { header('Content-type: image/' . $type); passthru(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($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(IMAGE_TRANSFORM_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/Drivers/NetPBM.php +++ pear/Image_Transform/Drivers/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: NetPBM.php,v 1.1 2002/05/31 18:05:56 reywob Exp $ // // Image Transformation interface using command line NetPBM // require_once "Image/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) { if (!defined('IMAGE_TRANSFORM_LIB_PATH')) { include_once 'System/Command.php'; define('IMAGE_TRANSFORM_LIB_PATH', escapeshellcmd(System_Command::which('pnmscale')) . '/'); } $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"; $this->_set_new_x($new_x); $this->_set_new_y($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); $this->command = array(); } // 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); $this->command = array(); } /** * 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->_set_img_x($size[0]); $this->_set_img_y($size[1]); } // End _get_size /** * get the image type * * @return string (gif,jpeg,png) */ function _get_type() { // Should 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 (#4322) next »