| [ 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 // $Id: Transform.php 19365 2005-10-11 22:16:35Z nelius_weiss $ 29 // 30 // Image Transformation interface 31 // 32 33 34 /** 35 * The main "Image_Resize" class is a container and base class which 36 * provides the static methods for creating Image objects as well as 37 * some utility functions (maths) common to all parts of Image Resize. 38 * 39 * The object model of DB is as follows (indentation means inheritance): 40 * 41 * Image_Resize The base for each Image implementation. Provides default 42 * | implementations (in OO lingo virtual methods) for 43 * | the actual Image implementations as well as a bunch of 44 * | maths methods. 45 * | 46 * +-Image_GD The Image implementation for the PHP GD extension . Inherits 47 * Image_Resize 48 * When calling DB::setup for GD images the object returned is an 49 * instance of this class. 50 * 51 * @package Image Resize 52 * @version 1.00 53 * @author Peter Bowyer <peter@mapledesign.co.uk> 54 * @since PHP 4.0 55 **/ 56 class Image_Transform 57 { 58 /** 59 * Name of the image file 60 * @var string 61 */ 62 var $image = ''; 63 /** 64 * Type of the image file (eg. jpg, gif png ...) 65 * @var string 66 */ 67 var $type = ''; 68 /** 69 * Original image width in x direction 70 * @var int 71 */ 72 var $img_x = ''; 73 /** 74 * Original image width in y direction 75 * @var int 76 */ 77 var $img_y = ''; 78 /** 79 * New image width in x direction 80 * @var int 81 */ 82 var $new_x = ''; 83 /** 84 * New image width in y direction 85 * @var int 86 */ 87 var $new_y = ''; 88 /** 89 * Path the the library used 90 * e.g. /usr/local/ImageMagick/bin/ or 91 * /usr/local/netpbm/ 92 */ 93 var $lib_path = ''; 94 /** 95 * Flag to warn if image has been resized more than once before displaying 96 * or saving. 97 */ 98 var $resized = false; 99 100 101 var $uid = ''; 102 103 var $lapse_time =900; //15 mins 104 105 /** 106 * Create a new Image_resize object 107 * 108 * @param string $driver name of driver class to initialize 109 * 110 * @return mixed a newly created Image_Transform object, or a PEAR 111 * error object on error 112 * 113 * @see PEAR::isError() 114 * @see Image_Transform::setOption() 115 */ 116 function &factory($driver) 117 { 118 if ('' == $driver) { 119 die("No image library specified... aborting. You must call ::factory() with one parameter, the library to load."); 120 121 } 122 // $this->uid = md5($_SERVER['REMOTE_ADDR']); 123 124 include_once "$driver.php"; 125 126 $classname = "Image_Transform_Driver_{$driver}"; 127 $obj =& new $classname; 128 return $obj; 129 } 130 131 132 /** 133 * Resize the Image in the X and/or Y direction 134 * If either is 0 it will be scaled proportionally 135 * 136 * @access public 137 * 138 * @param mixed $new_x (0, number, percentage 10% or 0.1) 139 * @param mixed $new_y (0, number, percentage 10% or 0.1) 140 * 141 * @return mixed none or PEAR_error 142 */ 143 function resize($new_x = 0, $new_y = 0) 144 { 145 // 0 means keep original size 146 $new_x = (0 == $new_x) ? $this->img_x : $this->_parse_size($new_x, $this->img_x); 147 $new_y = (0 == $new_y) ? $this->img_y : $this->_parse_size($new_y, $this->img_y); 148 // Now do the library specific resizing. 149 return $this->_resize($new_x, $new_y); 150 } // End resize 151 152 153 /** 154 * Scale the image to have the max x dimension specified. 155 * 156 * @param int $new_x Size to scale X-dimension to 157 * @return none 158 */ 159 function scaleMaxX($new_x) 160 { 161 $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); 162 return $this->_resize($new_x, $new_y); 163 } // End resizeX 164 165 /** 166 * Scale the image to have the max y dimension specified. 167 * 168 * @access public 169 * @param int $new_y Size to scale Y-dimension to 170 * @return none 171 */ 172 function scaleMaxY($new_y) 173 { 174 $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); 175 return $this->_resize($new_x, $new_y); 176 } // End resizeY 177 178 /** 179 * Scale Image to a maximum or percentage 180 * 181 * @access public 182 * @param mixed (number, percentage 10% or 0.1) 183 * @return mixed none or PEAR_error 184 */ 185 function scale($size) 186 { 187 if ((strlen($size) > 1) && (substr($size,-1) == '%')) { 188 return $this->scaleByPercentage(substr($size, 0, -1)); 189 } elseif ($size < 1) { 190 return $this->scaleByFactor($size); 191 } else { 192 return $this->scaleByLength($size); 193 } 194 } // End scale 195 196 /** 197 * Scales an image to a percentage of its original size. For example, if 198 * my image was 640x480 and I called scaleByPercentage(10) then the image 199 * would be resized to 64x48 200 * 201 * @access public 202 * @param int $size Percentage of original size to scale to 203 * @return none 204 */ 205 function scaleByPercentage($size) 206 { 207 return $this->scaleByFactor($size / 100); 208 } // End scaleByPercentage 209 210 /** 211 * Scales an image to a factor of its original size. For example, if 212 * my image was 640x480 and I called scaleByFactor(0.5) then the image 213 * would be resized to 320x240. 214 * 215 * @access public 216 * @param float $size Factor of original size to scale to 217 * @return none 218 */ 219 function scaleByFactor($size) 220 { 221 $new_x = round($size * $this->img_x, 0); 222 $new_y = round($size * $this->img_y, 0); 223 return $this->_resize($new_x, $new_y); 224 } // End scaleByFactor 225 226 /** 227 * Scales an image so that the longest side has this dimension. 228 * 229 * @access public 230 * @param int $size Max dimension in pixels 231 * @return none 232 */ 233 function scaleByLength($size) 234 { 235 if ($this->img_x >= $this->img_y) { 236 $new_x = $size; 237 $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); 238 } else { 239 $new_y = $size; 240 $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); 241 } 242 return $this->_resize($new_x, $new_y); 243 } // End scaleByLength 244 245 246 /** 247 * 248 * @access public 249 * @return void 250 */ 251 function _get_image_details($image) 252 { 253 //echo $image; 254 $data = @GetImageSize($image); 255 #1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order, 256 # 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC 257 if (is_array($data)){ 258 switch($data[2]){ 259 case 1: 260 $type = 'gif'; 261 break; 262 case 2: 263 $type = 'jpeg'; 264 break; 265 case 3: 266 $type = 'png'; 267 break; 268 case 4: 269 $type = 'swf'; 270 break; 271 case 5: 272 $type = 'psd'; 273 case 6: 274 $type = 'bmp'; 275 case 7: 276 case 8: 277 $type = 'tiff'; 278 default: 279 echo("We do not recognize this image format"); 280 } 281 $this->img_x = $data[0]; 282 $this->img_y = $data[1]; 283 $this->type = $type; 284 285 return true; 286 } else { 287 echo("Cannot fetch image or images details."); 288 return null; 289 } 290 /* 291 $output = array( 292 'width' => $data[0], 293 'height' => $data[1], 294 'type' => $type 295 ); 296 return $output; 297 */ 298 } 299 300 301 /** 302 * Parse input and convert 303 * If either is 0 it will be scaled proportionally 304 * 305 * @access private 306 * 307 * @param mixed $new_size (0, number, percentage 10% or 0.1) 308 * @param int $old_size 309 * 310 * @return mixed none or PEAR_error 311 */ 312 function _parse_size($new_size, $old_size) 313 { 314 if ('%' == $new_size) { 315 $new_size = str_replace('%','',$new_size); 316 $new_size = $new_size / 100; 317 } 318 if ($new_size > 1) { 319 return (int) $new_size; 320 } elseif ($new_size == 0) { 321 return (int) $old_size; 322 } else { 323 return (int) round($new_size * $old_size, 0); 324 } 325 } 326 327 328 function uniqueStr() 329 { 330 return substr(md5(microtime()),0,6); 331 } 332 333 //delete old tmp files, and allow only 1 file per remote host. 334 function cleanUp($id, $dir) 335 { 336 $d = dir($dir); 337 $id_length = strlen($id); 338 339 while (false !== ($entry = $d->read())) { 340 if (is_file($dir.'/'.$entry) && substr($entry,0,1) == '.' && !ereg($entry, $this->image)) 341 { 342 //echo filemtime($this->directory.'/'.$entry)."<br>"; 343 //echo time(); 344 345 if (filemtime($dir.'/'.$entry) + $this->lapse_time < time()) 346 unlink($dir.'/'.$entry); 347 348 if (substr($entry, 1, $id_length) == $id) 349 { 350 if (is_file($dir.'/'.$entry)) 351 unlink($dir.'/'.$entry); 352 } 353 } 354 } 355 $d->close(); 356 } 357 358 359 function createUnique($dir) 360 { 361 $unique_str = '.'.$this->uid.'_'.$this->uniqueStr().".".$this->type; 362 363 //make sure the the unique temp file does not exists 364 while (file_exists($dir.$unique_str)) 365 { 366 $unique_str = '.'.$this->uid.'_'.$this->uniqueStr().".".$this->type; 367 } 368 369 $this->cleanUp($this->uid, $dir); 370 371 return $unique_str; 372 } 373 374 375 /** 376 * Set the image width 377 * @param int $size dimension to set 378 * @since 29/05/02 13:36:31 379 * @return 380 */ 381 function _set_img_x($size) 382 { 383 $this->img_x = $size; 384 } 385 386 /** 387 * Set the image height 388 * @param int $size dimension to set 389 * @since 29/05/02 13:36:31 390 * @return 391 */ 392 function _set_img_y($size) 393 { 394 $this->img_y = $size; 395 } 396 397 /** 398 * Set the image width 399 * @param int $size dimension to set 400 * @since 29/05/02 13:36:31 401 * @return 402 */ 403 function _set_new_x($size) 404 { 405 $this->new_x = $size; 406 } 407 408 /** 409 * Set the image height 410 * @param int $size dimension to set 411 * @since 29/05/02 13:36:31 412 * @return 413 */ 414 function _set_new_y($size) 415 { 416 $this->new_y = $size; 417 } 418 419 /** 420 * Get the type of the image being manipulated 421 * 422 * @return string $this->type the image type 423 */ 424 function getImageType() 425 { 426 return $this->type; 427 } 428 429 /** 430 * 431 * @access public 432 * @return string web-safe image type 433 */ 434 function getWebSafeFormat() 435 { 436 switch($this->type){ 437 case 'gif': 438 case 'png': 439 return 'png'; 440 break; 441 default: 442 return 'jpeg'; 443 } // switch 444 } 445 446 /** 447 * Place holder for the real resize method 448 * used by extended methods to do the resizing 449 * 450 * @access private 451 * @return PEAR_error 452 */ 453 function _resize() { 454 return null; //PEAR::raiseError("No Resize method exists", true); 455 } 456 457 /** 458 * Place holder for the real load method 459 * used by extended methods to do the resizing 460 * 461 * @access public 462 * @return PEAR_error 463 */ 464 function load($filename) { 465 return null; //PEAR::raiseError("No Load method exists", true); 466 } 467 468 /** 469 * Place holder for the real display method 470 * used by extended methods to do the resizing 471 * 472 * @access public 473 * @param string filename 474 * @return PEAR_error 475 */ 476 function display($type, $quality) { 477 return null; //PEAR::raiseError("No Display method exists", true); 478 } 479 480 /** 481 * Place holder for the real save method 482 * used by extended methods to do the resizing 483 * 484 * @access public 485 * @param string filename 486 * @return PEAR_error 487 */ 488 function save($filename, $type, $quality) { 489 return null; //PEAR::raiseError("No Save method exists", true); 490 } 491 492 /** 493 * Place holder for the real free method 494 * used by extended methods to do the resizing 495 * 496 * @access public 497 * @return PEAR_error 498 */ 499 function free() { 500 return null; //PEAR::raiseError("No Free method exists", true); 501 } 502 503 /** 504 * Reverse of rgb2colorname. 505 * 506 * @access public 507 * @return PEAR_error 508 * 509 * @see rgb2colorname 510 */ 511 function colorhex2colorarray($colorhex) { 512 $r = hexdec(substr($colorhex, 1, 2)); 513 $g = hexdec(substr($colorhex, 3, 2)); 514 $b = hexdec(substr($colorhex, 4, 2)); 515 return array($r,$g,$b); 516 } 517 518 /** 519 * Reverse of rgb2colorname. 520 * 521 * @access public 522 * @return PEAR_error 523 * 524 * @see rgb2colorname 525 */ 526 function colorarray2colorhex($color) { 527 $color = '#'.dechex($color[0]).dechex($color[1]).dechex($color[2]); 528 return strlen($color)>6?false:$color; 529 } 530 531 532 /* Methods to add to the driver classes in the future */ 533 function addText() 534 { 535 return null; //PEAR::raiseError("No addText method exists", true); 536 } 537 538 function addDropShadow() 539 { 540 return null; //PEAR::raiseError("No AddDropShadow method exists", true); 541 } 542 543 function addBorder() 544 { 545 return null; //PEAR::raiseError("No addBorder method exists", true); 546 } 547 548 function crop() 549 { 550 return null; //PEAR::raiseError("No crop method exists", true); 551 } 552 553 function flip() 554 { 555 return null; 556 } 557 558 function gamma() 559 { 560 return null; //PEAR::raiseError("No gamma method exists", true); 561 } 562 } 563 ?>
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 |