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