[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once 'Horde/Image.php'; 4 5 /** 6 * This class implements the Horde_Image:: API for ImageMagick. 7 * 8 * $Horde: framework/Image/Image/im.php,v 1.34.10.11 2006/01/01 21:28:22 jan Exp $ 9 * 10 * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org> 11 * Copyright 2003-2006 Mike Cochrane <mike@graftonhall.co.nz> 12 * 13 * See the enclosed file COPYING for license information (LGPL). If you 14 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 15 * 16 * @author Chuck Hagenbuch <chuck@horde.org> 17 * @author Mike Cochrane <mike@graftonhall.co.nz> 18 * @since Horde 3.0 19 * @package Horde_Image 20 */ 21 class Horde_Image_im extends Horde_Image { 22 23 /** 24 * Capabilites of this driver. 25 * 26 * @var array 27 */ 28 var $_capabilities = array('resize', 29 'crop', 30 'rotate', 31 'grayscale', 32 'flip', 33 'mirror', 34 'sepia', 35 'canvas' 36 ); 37 38 /** 39 * What kind of images should ImageMagick generate? Defaults to 'png'. 40 * 41 * @var string 42 */ 43 var $_type = 'png'; 44 45 /** 46 * Operations to be performed. 47 * 48 * @var array 49 */ 50 var $_operations = array(); 51 52 /** 53 * Current stroke color; cached so we don't issue more -stroke commands 54 * than necessary. 55 * 56 * @var string 57 */ 58 var $_strokeColor = null; 59 60 /** 61 * Current stroke width; cached so we don't issue more -strokewidth 62 * commands than necessary. 63 * 64 * @var string 65 */ 66 var $_strokeWidth = null; 67 68 /** 69 * Current fill color; cached so we don't issue more -fill commands than 70 * necessary. 71 * 72 * @var string 73 */ 74 var $_fillColor = null; 75 76 /** 77 * Constructor. 78 */ 79 function Horde_Image_im($params) 80 { 81 parent::Horde_Image($params); 82 83 if (!empty($params['type'])) { 84 $this->_type = $params['type']; 85 } 86 87 // Make sure we start with a white background to be consistent 88 // with other drivers. 89 if (!empty($params['background'])) { 90 $bg = $params['background']; 91 } else { 92 $bg = 'white'; 93 } 94 $this->rectangle(0, 0, $this->_width, $this->_height, $bg, $bg); 95 } 96 97 /** 98 * Return the content type for this image. 99 * 100 * @return string The content type for this image. 101 */ 102 function getContentType() 103 { 104 return 'image/' . $this->_type; 105 } 106 107 /** 108 * Returns the raw data for this image. 109 * 110 * @param boolean $convert If true, the image data will be returned in the 111 * target format, independently from any image 112 * operations. 113 * 114 * @return string The raw image data. 115 */ 116 function raw($convert = false) 117 { 118 global $conf; 119 120 if (!empty($this->_data)) { 121 // If there are no operations, and we already have data, 122 // don't bother writing out files, just return the current 123 // data. 124 if (!$convert && !count($this->_operations)) { 125 return $this->_data; 126 } 127 128 $tmpin = $this->toFile($this->_data); 129 } else { 130 // Create an empty PPM file to load. 131 $tmpin = Util::getTempFile('img', false, $this->_tmpdir); 132 $fp = fopen($tmpin, 'wb'); 133 fwrite($fp, sprintf("P3\n%d %d\n255\n ", $this->_width, $this->_height)); 134 fclose($fp); 135 } 136 137 $tmpout = Util::getTempFile('img', false, $this->_tmpdir); 138 139 $command = $conf['image']['convert']; 140 $command .= ' ' . implode(' ', $this->_operations); 141 $command .= ' "' . $tmpin . '" +profile "*" ' . $this->_type . ':"' . $tmpout . '" 2>&1'; 142 143 exec($command, $output, $retval); 144 145 $this->_data = file_get_contents($tmpout); 146 147 @unlink($tmpin); 148 @unlink($tmpout); 149 150 return $this->_data; 151 } 152 153 /** 154 * Reset the image data. 155 */ 156 function reset() 157 { 158 parent::reset(); 159 $this->_operations = array(); 160 } 161 162 /** 163 * Resize the current image. 164 * 165 * @param integer $width The new width. 166 * @param integer $height The new height. 167 * @param boolean $ratio Maintain original aspect ratio. 168 */ 169 function resize($width, $height, $ratio = true) 170 { 171 if ($ratio) { 172 $this->_operations[] = "-size {$width}x{$height} -resize {$width}x{$height}"; 173 } else { 174 $this->_operations[] = "-size {$width}x{$height} -resize {$width}x{$height}!"; 175 } 176 } 177 178 /** 179 * Crop the current image. 180 * 181 * @param integer $x1 The top left corner of the cropped image. 182 * @param integer $y1 The top right corner of the cropped image. 183 * @param integer $x2 The bottom left corner of the cropped image. 184 * @param integer $y2 The bottom right corner of the cropped image. 185 */ 186 function crop($x1, $y1, $x2, $y2) 187 { 188 $line = ($x2 - $x1) . 'x' . ($y2 - $y1) . '+' . $x1 . '+' . $y2; 189 $this->_operations[] = '-crop ' . $line; 190 } 191 192 /** 193 * Rotate the current image. 194 * 195 * @param integer $angle The angle to rotate the image by, 196 * in the clockwise direction. 197 * @param integer $background The background color to fill any triangles. 198 */ 199 function rotate($angle, $background = 'white') 200 { 201 $this->_operations[] = "-rotate {$angle} -background $background"; 202 } 203 204 /** 205 * Flip the current image. 206 */ 207 function flip() 208 { 209 $this->_operations[] = '-flip'; 210 } 211 212 /** 213 * Mirror the current image. 214 */ 215 function mirror() 216 { 217 $this->_operations[] = '-flop'; 218 } 219 220 /** 221 * Convert the current image to grayscale. 222 */ 223 function grayscale() 224 { 225 $this->_operations[] = '-colorspace GRAY -colors 256'; 226 } 227 228 /** 229 * Sepia filter. 230 */ 231 function sepia() 232 { 233 $this->_operations[] = '-modulate 110 -colorspace GRAY -colors 256 -gamma 1.25/1/0.66'; 234 } 235 236 /** 237 * Draws a text string on the image in a specified location, with 238 * the specified style information. 239 * 240 * @param string $text The text to draw. 241 * @param integer $x The left x coordinate of the start of the text string. 242 * @param integer $y The top y coordinate of the start of the text string. 243 * @param string $font The font identifier you want to use for the text. 244 * @param string $color The color that you want the text displayed in. 245 * @param integer $direction An integer that specifies the orientation of the text. 246 */ 247 function text($string, $x, $y, $font = '_sans', $color = 'black', $direction = 0) 248 { 249 $this->setStrokeColor($color); 250 $this->setStrokeWidth(1); 251 252 $string = addslashes('"' . $string . '"'); 253 $y = $y + 12; 254 $this->_operations[] = "-draw \"text $x,$y $string\""; 255 } 256 257 /** 258 * Draw a circle. 259 * 260 * @param integer $x The x coordinate of the centre. 261 * @param integer $y The y coordinate of the centre. 262 * @param integer $r The radius of the circle. 263 * @param string $color The line color of the circle. 264 * @param string $fill The color to fill the circle. 265 */ 266 function circle($x, $y, $r, $color, $fill = 'none') 267 { 268 $this->setStrokeColor($color); 269 $this->setFillColor($fill); 270 271 $xMax = $x + $r; 272 $this->_operations[] = "-draw \"circle $x,$y $xMax,$y\""; 273 } 274 275 /** 276 * Draw a polygon based on a set of vertices. 277 * 278 * @param array $vertices An array of x and y labeled arrays 279 * (eg. $vertices[0]['x'], $vertices[0]['y'], ...). 280 * @param string $color The color you want to draw the polygon with. 281 * @param string $fill The color to fill the polygon. 282 */ 283 function polygon($verts, $color, $fill = 'none') 284 { 285 $this->setStrokeColor($color); 286 $this->setFillColor($fill); 287 288 $command = ''; 289 foreach ($verts as $vert) { 290 $command .= sprintf(' %d,%d', $vert['x'], $vert['y']); 291 } 292 $this->_operations[] = "-draw \"polygon $command\""; 293 } 294 295 /** 296 * Draw a rectangle. 297 * 298 * @param integer $x The left x-coordinate of the rectangle. 299 * @param integer $y The top y-coordinate of the rectangle. 300 * @param integer $width The width of the rectangle. 301 * @param integer $height The height of the rectangle. 302 * @param string $color The line color of the rectangle. 303 * @param string $fill The color to fill the rectangle. 304 */ 305 function rectangle($x, $y, $width, $height, $color, $fill = 'none') 306 { 307 $this->setStrokeColor($color); 308 $this->setFillColor($fill); 309 310 $xMax = $x + $width; 311 $yMax = $y + $height; 312 $this->_operations[] = "-draw \"rectangle $x,$y $xMax,$yMax\""; 313 } 314 315 /** 316 * Draw a rounded rectangle. 317 * 318 * @param integer $x The left x-coordinate of the rectangle. 319 * @param integer $y The top y-coordinate of the rectangle. 320 * @param integer $width The width of the rectangle. 321 * @param integer $height The height of the rectangle. 322 * @param integer $round The width of the corner rounding. 323 * @param string $color The line color of the rectangle. 324 * @param string $fill The color to fill the rounded rectangle with. 325 */ 326 function roundedRectangle($x, $y, $width, $height, $round, $color, $fill) 327 { 328 $this->setStrokeColor($color); 329 $this->setFillColor($fill); 330 331 $x1 = $x + $width; 332 $y1 = $y + $height; 333 $this->_operations[] = "-draw \"roundRectangle $x,$y $x1,$y1, $round,$round\""; 334 } 335 336 /** 337 * Draw a line. 338 * 339 * @param integer $x0 The x coordinate of the start. 340 * @param integer $y0 The y coordinate of the start. 341 * @param integer $x1 The x coordinate of the end. 342 * @param integer $y1 The y coordinate of the end. 343 * @param string $color The line color. 344 * @param string $width The width of the line. 345 */ 346 function line($x0, $y0, $x1, $y1, $color = 'black', $width = 1) 347 { 348 $this->setStrokeColor($color); 349 $this->setStrokeWidth($width); 350 $this->_operations[] = "-draw \"line $x0,$y0 $x1,$y1\""; 351 } 352 353 /** 354 * Draw a dashed line. 355 * 356 * @param integer $x0 The x co-ordinate of the start. 357 * @param integer $y0 The y co-ordinate of the start. 358 * @param integer $x1 The x co-ordinate of the end. 359 * @param integer $y1 The y co-ordinate of the end. 360 * @param string $color The line color. 361 * @param string $width The width of the line. 362 * @param integer $dash_length The length of a dash on the dashed line 363 * @param integer $dash_space The length of a space in the dashed line 364 */ 365 function dashedLine($x0, $y0, $x1, $y1, $color = 'black', $width = 1, $dash_length = 2, $dash_space = 2) 366 { 367 $this->setStrokeColor($color); 368 $this->setStrokeWidth($width); 369 $this->_operations[] = "-draw \"line $x0,$y0 $x1,$y1\""; 370 } 371 372 /** 373 * Draw a polyline (a non-closed, non-filled polygon) based on a 374 * set of vertices. 375 * 376 * @param array $vertices An array of x and y labeled arrays 377 * (eg. $vertices[0]['x'], $vertices[0]['y'], ...). 378 * @param string $color The color you want to draw the line with. 379 * @param string $width The width of the line. 380 */ 381 function polyline($verts, $color, $width = 1) 382 { 383 $this->setStrokeColor($color); 384 $this->setStrokeWidth($width); 385 $this->setFillColor('none'); 386 387 $command = ''; 388 foreach ($verts as $vert) { 389 $command .= sprintf(' %d,%d', $vert['x'], $vert['y']); 390 } 391 $this->_operations[] = "-draw \"polyline $command\""; 392 } 393 394 /** 395 * Draw an arc. 396 * 397 * @param integer $x The x coordinate of the centre. 398 * @param integer $y The y coordinate of the centre. 399 * @param integer $r The radius of the arc. 400 * @param integer $start The start angle of the arc. 401 * @param integer $end The end angle of the arc. 402 * @param string $color The line color of the arc. 403 * @param string $fill The fill color of the arc (defaults to none). 404 */ 405 function arc($x, $y, $r, $start, $end, $color = 'black', $fill = 'none') 406 { 407 $this->setStrokeColor($color); 408 $this->setFillColor($fill); 409 410 // Split up arcs greater than 180 degrees into two pieces. 411 $mid = round(($start + $end) / 2); 412 $x = round($x); 413 $y = round($y); 414 $r = round($r); 415 if ($mid > 90) { 416 $this->_operations[] = "-draw \"ellipse $x,$y $r,$r $start,$mid\""; 417 $this->_operations[] = "-draw \"ellipse $x,$y $r,$r $mid,$end\""; 418 } else { 419 $this->_operations[] = "-draw \"ellipse $x,$y $r,$r $start,$end\""; 420 } 421 422 // If filled, draw the outline. 423 if (!empty($fill)) { 424 list($x1, $y1) = $this->_circlePoint($start, $r * 2); 425 list($x2, $y2) = $this->_circlePoint($mid, $r * 2); 426 list($x3, $y3) = $this->_circlePoint($end, $r * 2); 427 428 // This seems to result in slightly better placement of 429 // pie slices. 430 $x++; 431 $y++; 432 433 $verts = array(array('x' => $x + $x3, 'y' => $y + $y3), 434 array('x' => $x, 'y' => $y), 435 array('x' => $x + $x1, 'y' => $y + $y1)); 436 437 if ($mid > 90) { 438 $verts1 = array(array('x' => $x + $x2, 'y' => $y + $y2), 439 array('x' => $x, 'y' => $y), 440 array('x' => $x + $x1, 'y' => $y + $y1)); 441 $verts2 = array(array('x' => $x + $x3, 'y' => $y + $y3), 442 array('x' => $x, 'y' => $y), 443 array('x' => $x + $x2, 'y' => $y + $y2)); 444 445 $this->polygon($verts1, $fill, $fill); 446 $this->polygon($verts2, $fill, $fill); 447 } else { 448 $this->polygon($verts, $fill, $fill); 449 } 450 451 $this->polyline($verts, $color); 452 } 453 } 454 455 /** 456 * Change the current stroke color. Will only affect the command 457 * string if $stroke is different from the previous stroke color 458 * (stored in $this->_strokeColor). 459 * 460 * @access private 461 * @see $_strokeColor 462 * 463 * @param string $color The new stroke color. 464 */ 465 function setStrokeColor($color) 466 { 467 if ($color != $this->_strokeColor) { 468 $this->_operations[] = "-stroke $color"; 469 $this->_strokeColor = $color; 470 } 471 } 472 473 /** 474 * Change the current stroke width. Will only affect the command 475 * string if $width is different from the previous stroke width 476 * (stored in $this->_strokeWidth). 477 * 478 * @access private 479 * @see $_stroke 480 * 481 * @param string $width The new stroke width. 482 */ 483 function setStrokeWidth($width) 484 { 485 if ($width != $this->_strokeWidth) { 486 $this->_operations[] = "-strokewidth $width"; 487 $this->_strokeWidth = $width; 488 } 489 } 490 491 /** 492 * Change the current fill color. Will only affect the command 493 * string if $color is different from the previous fill color 494 * (stored in $this->_fillColor). 495 * 496 * @access private 497 * @see $_fill 498 * 499 * @param string $color The new fill color. 500 */ 501 function setFillColor($color) 502 { 503 if ($color != $this->_fillColor) { 504 $this->_operations[] = "-fill $color"; 505 $this->_fillColor = $color; 506 } 507 } 508 509 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |