[2] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /*********************************************************************** |
---|
| 4 | ** Title.........: ImageMagick Driver |
---|
| 5 | ** Version.......: 1.0 |
---|
| 6 | ** Author........: Xiang Wei ZHUO <wei@zhuo.org> |
---|
| 7 | ** Filename......: IM.php |
---|
| 8 | ** Last changed..: 30 Aug 2003 |
---|
| 9 | ** Notes.........: Orginal is from PEAR |
---|
| 10 | **/ |
---|
| 11 | |
---|
| 12 | // +----------------------------------------------------------------------+ |
---|
| 13 | // | PHP Version 4 | |
---|
| 14 | // +----------------------------------------------------------------------+ |
---|
| 15 | // | Copyright (c) 1997-2002 The PHP Group | |
---|
| 16 | // +----------------------------------------------------------------------+ |
---|
| 17 | // | This source file is subject to version 2.02 of the PHP license, | |
---|
| 18 | // | that is bundled with this package in the file LICENSE, and is | |
---|
| 19 | // | available at through the world-wide-web at | |
---|
| 20 | // | http://www.php.net/license/2_02.txt. | |
---|
| 21 | // | If you did not receive a copy of the PHP license and are unable to | |
---|
| 22 | // | obtain it through the world-wide-web, please send a note to | |
---|
| 23 | // | license@php.net so we can mail you a copy immediately. | |
---|
| 24 | // +----------------------------------------------------------------------+ |
---|
| 25 | // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | |
---|
| 26 | // +----------------------------------------------------------------------+ |
---|
| 27 | // |
---|
| 28 | // |
---|
| 29 | // Image Transformation interface using command line ImageMagick |
---|
| 30 | // |
---|
| 31 | |
---|
| 32 | require_once "Transform.php"; |
---|
| 33 | |
---|
| 34 | Class Image_Transform_Driver_IM extends Image_Transform |
---|
| 35 | { |
---|
| 36 | /** |
---|
| 37 | * associative array commands to be executed |
---|
| 38 | * @var array |
---|
| 39 | */ |
---|
| 40 | var $command = array(); |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * |
---|
| 44 | * |
---|
| 45 | */ |
---|
| 46 | function Image_Transform_Driver_IM() |
---|
| 47 | { |
---|
| 48 | return true; |
---|
| 49 | } // End Image_IM |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Load image |
---|
| 53 | * |
---|
| 54 | * @param string filename |
---|
| 55 | * |
---|
| 56 | * @return mixed none or a PEAR error object on error |
---|
| 57 | * @see PEAR::isError() |
---|
| 58 | */ |
---|
| 59 | function load($image) |
---|
| 60 | { |
---|
| 61 | |
---|
| 62 | $this->uid = md5($_SERVER['REMOTE_ADDR']); |
---|
| 63 | /*if (!file_exists($image)) { |
---|
| 64 | return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true); |
---|
| 65 | }*/ |
---|
| 66 | $this->image = $image; |
---|
| 67 | $this->_get_image_details($image); |
---|
| 68 | } // End load |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Resize Action |
---|
| 72 | * |
---|
| 73 | * @param int new_x new width |
---|
| 74 | * @param int new_y new height |
---|
| 75 | * |
---|
| 76 | * @return none |
---|
| 77 | * @see PEAR::isError() |
---|
| 78 | */ |
---|
| 79 | function _resize($new_x, $new_y) |
---|
| 80 | { |
---|
| 81 | /*if (isset($this->command['resize'])) { |
---|
| 82 | return PEAR::raiseError("You cannot scale or resize an image more than once without calling save or display", true); |
---|
| 83 | }*/ |
---|
| 84 | $this->command['resize'] = "-geometry ${new_x}x${new_y}!"; |
---|
| 85 | |
---|
| 86 | $this->new_x = $new_x; |
---|
| 87 | $this->new_y = $new_y; |
---|
| 88 | } // End resize |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Crop the image |
---|
| 92 | * |
---|
| 93 | * @param int $crop_x left column of the image |
---|
| 94 | * @param int $crop_y top row of the image |
---|
| 95 | * @param int $crop_width new cropped image width |
---|
| 96 | * @param int $crop_height new cropped image height |
---|
| 97 | */ |
---|
| 98 | function crop($crop_x, $crop_y, $crop_width, $crop_height) |
---|
| 99 | { |
---|
| 100 | $this->command['crop'] = "-crop {$crop_width}x{$crop_height}+{$crop_x}+{$crop_y}"; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Flip the image horizontally or vertically |
---|
| 105 | * |
---|
| 106 | * @param boolean $horizontal true if horizontal flip, vertical otherwise |
---|
| 107 | */ |
---|
| 108 | function flip($horizontal) |
---|
| 109 | { |
---|
| 110 | if($horizontal) |
---|
| 111 | $this->command['flop'] = "-flop"; |
---|
| 112 | else |
---|
| 113 | $this->command['flip'] = "-flip"; |
---|
| 114 | } |
---|
| 115 | /** |
---|
| 116 | * rotate |
---|
| 117 | * |
---|
| 118 | * @param int angle rotation angle |
---|
| 119 | * @param array options no option allowed |
---|
| 120 | * |
---|
| 121 | */ |
---|
| 122 | function rotate($angle, $options=null) |
---|
| 123 | { |
---|
| 124 | if ('-' == $angle{0}) { |
---|
| 125 | $angle = 360 - substr($angle, 1); |
---|
| 126 | } |
---|
| 127 | $this->command['rotate'] = "-rotate $angle"; |
---|
| 128 | } // End rotate |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * addText |
---|
| 132 | * |
---|
| 133 | * @param array options Array contains options |
---|
| 134 | * array( |
---|
| 135 | * 'text' The string to draw |
---|
| 136 | * 'x' Horizontal position |
---|
| 137 | * 'y' Vertical Position |
---|
| 138 | * 'Color' Font color |
---|
| 139 | * 'font' Font to be used |
---|
| 140 | * 'size' Size of the fonts in pixel |
---|
| 141 | * 'resize_first' Tell if the image has to be resized |
---|
| 142 | * before drawing the text |
---|
| 143 | * ) |
---|
| 144 | * |
---|
| 145 | * @return none |
---|
| 146 | * @see PEAR::isError() |
---|
| 147 | */ |
---|
| 148 | function addText($params) |
---|
| 149 | { |
---|
| 150 | $default_params = array( |
---|
| 151 | 'text' => 'This is Text', |
---|
| 152 | 'x' => 10, |
---|
| 153 | 'y' => 20, |
---|
| 154 | 'color' => 'red', |
---|
| 155 | 'font' => 'Arial.ttf', |
---|
| 156 | 'resize_first' => false // Carry out the scaling of the image before annotation? |
---|
| 157 | ); |
---|
| 158 | $params = array_merge($default_params, $params); |
---|
| 159 | extract($params); |
---|
| 160 | if (true === $resize_first) { |
---|
| 161 | // Set the key so that this will be the last item in the array |
---|
| 162 | $key = 'ztext'; |
---|
| 163 | } else { |
---|
| 164 | $key = 'text'; |
---|
| 165 | } |
---|
| 166 | $this->command[$key] = "-font $font -fill $color -draw 'text $x,$y \"$text\"'"; |
---|
| 167 | // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory]. |
---|
| 168 | } // End addText |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Adjust the image gamma |
---|
| 172 | * |
---|
| 173 | * @param float $outputgamma |
---|
| 174 | * |
---|
| 175 | * @return none |
---|
| 176 | */ |
---|
| 177 | function gamma($outputgamma=1.0) { |
---|
| 178 | $this->command['gamma'] = "-gamma $outputgamma"; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Save the image file |
---|
| 183 | * |
---|
| 184 | * @param $filename string the name of the file to write to |
---|
| 185 | * @param $quality quality image dpi, default=75 |
---|
| 186 | * @param $type string (JPG,PNG...) |
---|
| 187 | * |
---|
| 188 | * @return none |
---|
| 189 | */ |
---|
| 190 | function save($filename, $type='', $quality = 85) |
---|
| 191 | { |
---|
| 192 | $type == '' ? $this->type : $type; |
---|
| 193 | $cmd = '' . IMAGE_TRANSFORM_LIB_PATH . 'convert" ' . implode(' ', $this->command) . " -quality $quality " . ($this->image) . ' ' . ($filename) . ' 2>&1'; |
---|
| 194 | |
---|
| 195 | //echo $cmd; |
---|
| 196 | exec($cmd); |
---|
| 197 | } // End save |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * Display image without saving and lose changes |
---|
| 201 | * |
---|
| 202 | * @param string type (JPG,PNG...); |
---|
| 203 | * @param int quality 75 |
---|
| 204 | * |
---|
| 205 | * @return none |
---|
| 206 | */ |
---|
| 207 | function display($type = '', $quality = 75) |
---|
| 208 | { |
---|
| 209 | if ($type == '') { |
---|
| 210 | header('Content-type: image/' . $this->type); |
---|
| 211 | passthru(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . strtoupper($this->type) . ":-"); |
---|
| 212 | } else { |
---|
| 213 | header('Content-type: image/' . $type); |
---|
| 214 | passthru(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality " . escapeshellarg($this->image) . ' ' . strtoupper($type) . ":-"); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Destroy image handle |
---|
| 221 | * |
---|
| 222 | * @return none |
---|
| 223 | */ |
---|
| 224 | function free() |
---|
| 225 | { |
---|
| 226 | return true; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | } // End class ImageIM |
---|
| 230 | ?> |
---|