[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 // +----------------------------------------------------------------------+ 3 // | PHP Version 4 | 4 // +----------------------------------------------------------------------+ 5 // | Copyright (c) 1997-2002 The PHP Group | 6 // +----------------------------------------------------------------------+ 7 // | This software is available under the two different licenses | 8 // | mentioned below. To use this software you must chose, and qualify, | 9 // |for one of those. | 10 // +----------------------------------------------------------------------+ 11 // | This source file is subject to version 2.02 of the PHP license, | 12 // | that is bundled with this package in the file LICENSE, and is | 13 // | available at through the world-wide-web at | 14 // | http://www.php.net/license/2_02.txt. | 15 // | If you did not receive a copy of the PHP license and are unable to | 16 // | obtain it through the world-wide-web, please send a note to | 17 // | license@php.net so we can mail you a copy immediately. | 18 // +----------------------------------------------------------------------+ 19 // | This program is free software; you can redistribute it and/or modify | 20 // | it under the terms of the GNU Lesser General Public License as | 21 // | published by the Free Software Foundation; version 2 of the License. | 22 // +----------------------------------------------------------------------+ 23 // | Authors: Peter Bowyer <peter@mapledesign.co.uk> | 24 // | Alan Knowles <alan@akbkhome.com> | 25 // | Vincent Oostindie <vincent@sunlight.tmfweb.nl> | 26 // +----------------------------------------------------------------------+// 27 // 28 // Image Transformation interface using the GD library 29 // 30 // 31 // $Id: GD.php 19365 2005-10-11 22:16:35Z nelius_weiss $ 32 33 34 require_once "Transform.php"; 35 36 Class Image_Transform_Driver_GD extends Image_Transform 37 { 38 /** 39 * Holds the image file for manipulation 40 */ 41 var $imageHandle = ''; 42 43 /** 44 * Holds the original image file 45 */ 46 var $old_image = ''; 47 48 /** 49 * Check settings 50 * 51 * @return mixed true or or a PEAR error object on error 52 * 53 * @see PEAR::isError() 54 */ 55 function Image_Transform_GD() 56 { 57 return; 58 } // End function Image 59 60 /** 61 * Load image 62 * 63 * @param string filename 64 * 65 * @return mixed none or a PEAR error object on error 66 * @see PEAR::isError() 67 */ 68 function load($image) 69 { 70 $this->uid = md5($_SERVER['REMOTE_ADDR']); 71 $this->image = $image; 72 $this->_get_image_details($image); 73 $functionName = 'ImageCreateFrom' . $this->type; 74 $this->imageHandle = $functionName($this->image); 75 } // End load 76 77 /** 78 * addText 79 * 80 * @param array options Array contains options 81 * array( 82 * 'text' The string to draw 83 * 'x' Horizontal position 84 * 'y' Vertical Position 85 * 'Color' Font color 86 * 'font' Font to be used 87 * 'size' Size of the fonts in pixel 88 * 'resize_first' Tell if the image has to be resized 89 * before drawing the text 90 * ) 91 * 92 * @return none 93 * @see PEAR::isError() 94 */ 95 function addText($params) 96 { 97 $default_params = array( 98 'text' => 'This is Text', 99 'x' => 10, 100 'y' => 20, 101 'color' => array(255,0,0), 102 'font' => 'Arial.ttf', 103 'size' => '12', 104 'angle' => 0, 105 'resize_first' => false // Carry out the scaling of the image before annotation? Not used for GD 106 ); 107 $params = array_merge($default_params, $params); 108 extract($params); 109 110 if( !is_array($color) ){ 111 if ($color[0]=='#'){ 112 $this->colorhex2colorarray( $color ); 113 } else { 114 include_once('Image/Transform/Driver/ColorsDefs.php'); 115 $color = isset($colornames[$color])?$colornames[$color]:false; 116 } 117 } 118 119 $c = imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]); 120 121 if ('ttf' == substr($font, -3)) { 122 ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); 123 } else { 124 ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); 125 } 126 return true; 127 } // End addText 128 129 130 /** 131 * Rotate image by the given angle 132 * Uses a fast rotation algorythm for custom angles 133 * or lines copy for multiple of 90 degrees 134 * 135 * @param int $angle Rotation angle 136 * @param array $options array( 'autoresize'=>true|false, 137 * 'color_mask'=>array(r,g,b), named color or #rrggbb 138 * ) 139 * @author Pierre-Alain Joye 140 * @return mixed none or a PEAR error object on error 141 * @see PEAR::isError() 142 */ 143 function rotate($angle, $options=null) 144 { 145 if(function_exists('imagerotate')) { 146 $white = imagecolorallocate ($this->imageHandle, 255, 255, 255); 147 $this->imageHandle = imagerotate($this->imageHandle, $angle, $white); 148 return true; 149 } 150 151 if ( $options==null ){ 152 $autoresize = true; 153 $color_mask = array(255,255,0); 154 } else { 155 extract( $options ); 156 } 157 158 while ($angle <= -45) { 159 $angle += 360; 160 } 161 while ($angle > 270) { 162 $angle -= 360; 163 } 164 165 $t = deg2rad($angle); 166 167 if( !is_array($color_mask) ){ 168 if ($color[0]=='#'){ 169 $this->colorhex2colorarray( $color_mask ); 170 } else { 171 include_once('Image/Transform/Driver/ColorDefs.php'); 172 $color = isset($colornames[$color_mask])?$colornames[$color_mask]:false; 173 } 174 } 175 176 // Do not round it, too much lost of quality 177 $cosT = cos($t); 178 $sinT = sin($t); 179 180 $img =& $this->imageHandle; 181 182 $width = $max_x = $this->img_x; 183 $height = $max_y = $this->img_y; 184 $min_y = 0; 185 $min_x = 0; 186 187 $x1 = round($max_x/2,0); 188 $y1 = round($max_y/2,0); 189 190 if ( $autoresize ){ 191 $t = abs($t); 192 $a = round($angle,0); 193 switch((int)($angle)){ 194 case 0: 195 $width2 = $width; 196 $height2 = $height; 197 break; 198 case 90: 199 $width2 = $height; 200 $height2 = $width; 201 break; 202 case 180: 203 $width2 = $width; 204 $height2 = $height; 205 break; 206 case 270: 207 $width2 = $height; 208 $height2 = $width; 209 break; 210 default: 211 $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); 212 $height2 = (int)(abs(cos($t) * $height+sin($t) * $width)); 213 } 214 215 $width2 -= $width2%2; 216 $height2 -= $height2%2; 217 218 $d_width = abs($width - $width2); 219 $d_height = abs($height - $height2); 220 $x_offset = $d_width/2; 221 $y_offset = $d_height/2; 222 $min_x2 = -abs($x_offset); 223 $min_y2 = -abs($y_offset); 224 $max_x2 = $width2; 225 $max_y2 = $height2; 226 } 227 228 $img2 = @imagecreate($width2,$height2); 229 230 if ( !is_resource($img2) ){ 231 return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.', 232 null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ 233 } 234 235 $this->img_x = $width2; 236 $this->img_y = $height2; 237 238 239 imagepalettecopy($img2,$img); 240 241 $mask = imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]); 242 243 // use simple lines copy for axes angles 244 switch((int)($angle)){ 245 case 0: 246 imagefill ($img2, 0, 0,$mask); 247 for ($y=0; $y < $max_y; $y++) { 248 for ($x = $min_x; $x < $max_x; $x++){ 249 $c = @imagecolorat ( $img, $x, $y); 250 imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); 251 } 252 } 253 break; 254 case 90: 255 imagefill ($img2, 0, 0,$mask); 256 for ($x = $min_x; $x < $max_x; $x++){ 257 for ($y=$min_y; $y < $max_y; $y++) { 258 $c = imagecolorat ( $img, $x, $y); 259 imagesetpixel($img2,$max_y-$y-1,$x,$c); 260 } 261 } 262 break; 263 case 180: 264 imagefill ($img2, 0, 0,$mask); 265 for ($y=0; $y < $max_y; $y++) { 266 for ($x = $min_x; $x < $max_x; $x++){ 267 $c = @imagecolorat ( $img, $x, $y); 268 imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c); 269 } 270 } 271 break; 272 case 270: 273 imagefill ($img2, 0, 0,$mask); 274 for ($y=0; $y < $max_y; $y++) { 275 for ($x = $max_x; $x >= $min_x; $x--){ 276 $c = @imagecolorat ( $img, $x, $y); 277 imagesetpixel($img2,$y,$max_x-$x-1,$c); 278 } 279 } 280 break; 281 // simple reverse rotation algo 282 default: 283 $i=0; 284 for ($y = $min_y2; $y < $max_y2; $y++){ 285 286 // Algebra :) 287 $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0); 288 $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0); 289 290 for ($x = $min_x2; $x < $max_x2; $x++){ 291 292 // Check if we are out of original bounces, if we are 293 // use the default color mask 294 if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){ 295 $c = imagecolorat ( $img, $x2, $y2); 296 } else { 297 $c = $mask; 298 } 299 imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c); 300 301 // round verboten! 302 $x2 += $cosT; 303 $y2 -= $sinT; 304 } 305 } 306 break; 307 } 308 $this->old_image = $this->imageHandle; 309 $this->imageHandle = $img2; 310 return true; 311 } 312 313 314 /** 315 * Resize Action 316 * 317 * For GD 2.01+ the new copyresampled function is used 318 * It uses a bicubic interpolation algorithm to get far 319 * better result. 320 * 321 * @param $new_x int new width 322 * @param $new_y int new height 323 * 324 * @return true on success or pear error 325 * @see PEAR::isError() 326 */ 327 function _resize($new_x, $new_y) { 328 if ($this->resized === true) { 329 return false; /*PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ 330 } 331 if(function_exists('ImageCreateTrueColor')){ 332 $new_img =ImageCreateTrueColor($new_x,$new_y); 333 } else { 334 $new_img =ImageCreate($new_x,$new_y); 335 } 336 if(function_exists('ImageCopyResampled')){ 337 ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); 338 } else { 339 ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); 340 } 341 $this->old_image = $this->imageHandle; 342 $this->imageHandle = $new_img; 343 $this->resized = true; 344 345 $this->new_x = $new_x; 346 $this->new_y = $new_y; 347 return true; 348 } 349 350 /** 351 * Crop the image 352 * 353 * @param int $crop_x left column of the image 354 * @param int $crop_y top row of the image 355 * @param int $crop_width new cropped image width 356 * @param int $crop_height new cropped image height 357 */ 358 function crop($new_x, $new_y, $new_width, $new_height) 359 { 360 if(function_exists('ImageCreateTrueColor')){ 361 $new_img =ImageCreateTrueColor($new_width,$new_height); 362 } else { 363 $new_img =ImageCreate($new_width,$new_height); 364 } 365 if(function_exists('ImageCopyResampled')){ 366 ImageCopyResampled($new_img, $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height); 367 } else { 368 ImageCopyResized($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height); 369 } 370 $this->old_image = $this->imageHandle; 371 $this->imageHandle = $new_img; 372 $this->resized = true; 373 374 $this->new_x = $new_x; 375 $this->new_y = $new_y; 376 return true; 377 } 378 379 /** 380 * Flip the image horizontally or vertically 381 * 382 * @param boolean $horizontal true if horizontal flip, vertical otherwise 383 */ 384 function flip($horizontal) 385 { 386 if(!$horizontal) { 387 $this->rotate(180); 388 } 389 390 $width = imagesx($this->imageHandle); 391 $height = imagesy($this->imageHandle); 392 393 for ($j = 0; $j < $height; $j++) { 394 $left = 0; 395 $right = $width-1; 396 397 398 while ($left < $right) { 399 //echo " j:".$j." l:".$left." r:".$right."\n<br>"; 400 $t = imagecolorat($this->imageHandle, $left, $j); 401 imagesetpixel($this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j)); 402 imagesetpixel($this->imageHandle, $right, $j, $t); 403 $left++; $right--; 404 } 405 } 406 407 return true; 408 } 409 410 411 /** 412 * Adjust the image gamma 413 * 414 * @param float $outputgamma 415 * 416 * @return none 417 */ 418 function gamma($outputgamma=1.0) { 419 ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma); 420 } 421 422 /** 423 * Save the image file 424 * 425 * @param $filename string the name of the file to write to 426 * @param $quality int output DPI, default is 85 427 * @param $types string define the output format, default 428 * is the current used format 429 * 430 * @return none 431 */ 432 function save($filename, $type = '', $quality = 85) 433 { 434 $type = $type==''? $this->type : $type; 435 $functionName = 'image' . $type; 436 $this->old_image = $this->imageHandle; 437 $functionName($this->imageHandle, $filename) ; 438 $this->imageHandle = $this->old_image; 439 $this->resized = false; 440 } // End save 441 442 443 /** 444 * Display image without saving and lose changes 445 * 446 * @param string type (JPG,PNG...); 447 * @param int quality 75 448 * 449 * @return none 450 */ 451 function display($type = '', $quality = 75) 452 { 453 if ($type != '') { 454 $this->type = $type; 455 } 456 $functionName = 'Image' . $this->type; 457 header('Content-type: image/' . strtolower($this->type)); 458 $functionName($this->imageHandle, '', $quality); 459 $this->imageHandle = $this->old_image; 460 $this->resized = false; 461 ImageDestroy($this->old_image); 462 $this->free(); 463 } 464 465 /** 466 * Destroy image handle 467 * 468 * @return none 469 */ 470 function free() 471 { 472 if ($this->imageHandle){ 473 ImageDestroy($this->imageHandle); 474 } 475 } 476 477 } // End class ImageGD 478 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |