[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/js/htmlarea/plugins/UploadImage/popups/ImageEditor/ -> GD.php (source)

   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 15443 2004-06-15 00:27:03Z mipmip $
  41  //
  42  // Image Transformation interface using the GD library
  43  //
  44  
  45  require_once  "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          $this->imageHandle = $functionName($this->image);
  86      } // End load
  87  
  88      /**
  89       * addText
  90       *
  91       * @param   array   options     Array contains options
  92       *                              array(
  93       *                                  'text'  The string to draw
  94       *                                  'x'     Horizontal position
  95       *                                  'y'     Vertical Position
  96       *                                  'Color' Font color
  97       *                                  'font'  Font to be used
  98       *                                  'size'  Size of the fonts in pixel
  99       *                                  'resize_first'  Tell if the image has to be resized
 100       *                                                  before drawing the text
 101       *                              )
 102       *
 103       * @return none
 104       * @see PEAR::isError()
 105       */
 106      function addText($params)
 107      {
 108          $default_params = array(
 109                                  'text' => 'This is Text',
 110                                  'x' => 10,
 111                                  'y' => 20,
 112                                  'color' => array(255,0,0),
 113                                  'font' => 'Arial.ttf',
 114                                  'size' => '12',
 115                                  'angle' => 0,
 116                                  'resize_first' => false // Carry out the scaling of the image before annotation?  Not used for GD
 117                                  );
 118          $params = array_merge($default_params, $params);
 119          extract($params);
 120  
 121          if( !is_array($color) ){
 122              if ($color[0]=='#'){
 123                  $this->colorhex2colorarray( $color );
 124              } else {
 125                  include_once('Image/Transform/Driver/ColorsDefs.php');
 126                  $color = isset($colornames[$color])?$colornames[$color]:false;
 127              }
 128          }
 129  
 130          $c = imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]);
 131  
 132          if ('ttf' == substr($font, -3)) {
 133              ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
 134          } else {
 135              ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
 136          }
 137          return true;
 138      } // End addText
 139  
 140  
 141      /**
 142       * Rotate image by the given angle
 143       * Uses a fast rotation algorythm for custom angles
 144       * or lines copy for multiple of 90 degrees
 145       *
 146       * @param int       $angle      Rotation angle
 147       * @param array     $options    array(  'autoresize'=>true|false,
 148       *                                      'color_mask'=>array(r,g,b), named color or #rrggbb
 149       *                                   )
 150       * @author Pierre-Alain Joye
 151       * @return mixed none or a PEAR error object on error
 152       * @see PEAR::isError()
 153       */
 154      function rotate($angle, $options=null)
 155      {
 156          if(function_exists('imagerotate')) {
 157              $white = imagecolorallocate ($this->imageHandle, 255, 255, 255);
 158              $this->imageHandle = imagerotate($this->imageHandle, $angle, $white);
 159              return true;
 160          }
 161  
 162          if ( $options==null ){
 163              $autoresize = true;
 164              $color_mask = array(255,255,0);
 165          } else {
 166              extract( $options );
 167          }
 168  
 169          while ($angle <= -45) {
 170              $angle  += 360;
 171          }
 172          while ($angle > 270) {
 173              $angle  -= 360;
 174          }
 175  
 176          $t      = deg2rad($angle);
 177  
 178          if( !is_array($color_mask) ){
 179              if ($color[0]=='#'){
 180                  $this->colorhex2colorarray( $color_mask );
 181              } else {
 182                  include_once('Image/Transform/Driver/ColorDefs.php');
 183                  $color = isset($colornames[$color_mask])?$colornames[$color_mask]:false;
 184              }
 185          }
 186  
 187          // Do not round it, too much lost of quality
 188          $cosT   = cos($t);
 189          $sinT   = sin($t);
 190  
 191          $img    =& $this->imageHandle;
 192  
 193          $width  = $max_x  = $this->img_x;
 194          $height = $max_y  = $this->img_y;
 195          $min_y  = 0;
 196          $min_x  = 0;
 197  
 198          $x1     = round($max_x/2,0);
 199          $y1     = round($max_y/2,0);
 200  
 201          if ( $autoresize ){
 202              $t      = abs($t);
 203              $a      = round($angle,0);
 204              switch((int)($angle)){
 205                  case 0:
 206                          $width2     = $width;
 207                          $height2    = $height;
 208                      break;
 209                  case 90:
 210                          $width2     = $height;
 211                          $height2    = $width;
 212                      break;
 213                  case 180:
 214                          $width2     = $width;
 215                          $height2    = $height;
 216                      break;
 217                  case 270:
 218                          $width2     = $height;
 219                          $height2    = $width;
 220                      break;
 221                  default:
 222                      $width2     = (int)(abs(sin($t) * $height + cos($t) * $width));
 223                      $height2    = (int)(abs(cos($t) * $height+sin($t) * $width));
 224              }
 225  
 226              $width2     -= $width2%2;
 227              $height2    -= $height2%2;
 228  
 229              $d_width    = abs($width - $width2);
 230              $d_height   = abs($height - $height2);
 231              $x_offset   = $d_width/2;
 232              $y_offset   = $d_height/2;
 233              $min_x2     = -abs($x_offset);
 234              $min_y2     = -abs($y_offset);
 235              $max_x2     = $width2;
 236              $max_y2     = $height2;
 237          }
 238  
 239          $img2   = @imagecreate($width2,$height2);
 240  
 241          if ( !is_resource($img2) ){
 242              return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.',
 243                                  null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
 244          }
 245  
 246          $this->img_x = $width2;
 247          $this->img_y = $height2;
 248  
 249  
 250          imagepalettecopy($img2,$img);
 251  
 252          $mask   = imagecolorresolve($img2,$color_mask[0],$color_mask[1],$color_mask[2]);
 253  
 254          // use simple lines copy for axes angles
 255          switch((int)($angle)){
 256              case 0:
 257                  imagefill ($img2, 0, 0,$mask);
 258                  for ($y=0; $y < $max_y; $y++) {
 259                      for ($x = $min_x; $x < $max_x; $x++){
 260                          $c  = @imagecolorat ( $img, $x, $y);
 261                          imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c);
 262                      }
 263                  }
 264                  break;
 265              case 90:
 266                  imagefill ($img2, 0, 0,$mask);
 267                  for ($x = $min_x; $x < $max_x; $x++){
 268                      for ($y=$min_y; $y < $max_y; $y++) {
 269                          $c  = imagecolorat ( $img, $x, $y);
 270                          imagesetpixel($img2,$max_y-$y-1,$x,$c);
 271                      }
 272                  }
 273                  break;
 274              case 180:
 275                  imagefill ($img2, 0, 0,$mask);
 276                  for ($y=0; $y < $max_y; $y++) {
 277                      for ($x = $min_x; $x < $max_x; $x++){
 278                          $c  = @imagecolorat ( $img, $x, $y);
 279                          imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c);
 280                      }
 281                  }
 282                  break;
 283              case 270:
 284                  imagefill ($img2, 0, 0,$mask);
 285                  for ($y=0; $y < $max_y; $y++) {
 286                      for ($x = $max_x; $x >= $min_x; $x--){
 287                          $c  = @imagecolorat ( $img, $x, $y);
 288                          imagesetpixel($img2,$y,$max_x-$x-1,$c);
 289                      }
 290                  }
 291                  break;
 292              // simple reverse rotation algo
 293              default:
 294                  $i=0;
 295                  for ($y = $min_y2; $y < $max_y2; $y++){
 296  
 297                      // Algebra :)
 298                      $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0);
 299                      $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0);
 300  
 301                      for ($x = $min_x2; $x < $max_x2; $x++){
 302  
 303                          // Check if we are out of original bounces, if we are
 304                          // use the default color mask
 305                          if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){
 306                              $c  = imagecolorat ( $img, $x2, $y2);
 307                          } else {
 308                              $c  = $mask;
 309                          }
 310                          imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c);
 311  
 312                          // round verboten!
 313                          $x2  += $cosT;
 314                          $y2  -= $sinT;
 315                      }
 316                  }
 317                  break;
 318          }
 319          $this->old_image    = $this->imageHandle;
 320          $this->imageHandle  =  $img2;
 321          return true;
 322      }
 323  
 324  
 325     /**
 326      * Resize Action
 327      *
 328      * For GD 2.01+ the new copyresampled function is used
 329      * It uses a bicubic interpolation algorithm to get far
 330      * better result.
 331      *
 332      * @param $new_x int  new width
 333      * @param $new_y int  new height
 334      *
 335      * @return true on success or pear error
 336      * @see PEAR::isError()
 337      */
 338      function _resize($new_x, $new_y) {
 339          if ($this->resized === true) {
 340              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);*/
 341          }
 342          if(function_exists('ImageCreateTrueColor')){
 343              $new_img =ImageCreateTrueColor($new_x,$new_y);
 344          } else {
 345              $new_img =ImageCreate($new_x,$new_y);
 346          }
 347          if(function_exists('ImageCopyResampled')){
 348              ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
 349          } else {
 350              ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
 351          }
 352          $this->old_image = $this->imageHandle;
 353          $this->imageHandle = $new_img;
 354          $this->resized = true;
 355  
 356          $this->new_x = $new_x;
 357          $this->new_y = $new_y;
 358          return true;
 359      }
 360  
 361      /**
 362       * Crop the image
 363       *
 364       * @param int $crop_x left column of the image
 365       * @param int $crop_y top row of the image
 366       * @param int $crop_width new cropped image width
 367       * @param int $crop_height new cropped image height
 368       */
 369      function crop($new_x, $new_y, $new_width, $new_height) 
 370      {
 371          if(function_exists('ImageCreateTrueColor')){
 372              $new_img =ImageCreateTrueColor($new_width,$new_height);
 373          } else {
 374              $new_img =ImageCreate($new_width,$new_height);
 375          }
 376          if(function_exists('ImageCopyResampled')){
 377              ImageCopyResampled($new_img, $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height);
 378          } else {
 379              ImageCopyResized($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height);
 380          }
 381          $this->old_image = $this->imageHandle;
 382          $this->imageHandle = $new_img;
 383          $this->resized = true;
 384  
 385          $this->new_x = $new_x;
 386          $this->new_y = $new_y;
 387          return true;
 388      }
 389     
 390      /**
 391       * Flip the image horizontally or vertically
 392       *
 393       * @param boolean $horizontal true if horizontal flip, vertical otherwise
 394       */
 395      function flip($horizontal)
 396      {
 397          if(!$horizontal) {
 398              $this->rotate(180);
 399          }
 400  
 401          $width = imagesx($this->imageHandle); 
 402          $height = imagesy($this->imageHandle); 
 403  
 404          for ($j = 0; $j < $height; $j++) { 
 405                  $left = 0; 
 406                  $right = $width-1; 
 407  
 408  
 409                  while ($left < $right) { 
 410                      //echo " j:".$j." l:".$left." r:".$right."\n<br>";
 411                      $t = imagecolorat($this->imageHandle, $left, $j); 
 412                      imagesetpixel($this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j)); 
 413                      imagesetpixel($this->imageHandle, $right, $j, $t); 
 414                      $left++; $right--; 
 415                  } 
 416          }
 417  
 418          return true;
 419      }
 420  
 421  
 422      /**
 423       * Adjust the image gamma
 424       *
 425       * @param float $outputgamma
 426       *
 427       * @return none
 428       */
 429      function gamma($outputgamma=1.0) {
 430          ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma);
 431      }
 432  
 433      /**
 434       * Save the image file
 435       *
 436       * @param $filename string  the name of the file to write to
 437       * @param $quality  int     output DPI, default is 85
 438       * @param $types    string  define the output format, default
 439       *                          is the current used format
 440       *
 441       * @return none
 442       */
 443      function save($filename, $type = '', $quality = 85)
 444      {
 445          $type           = $type==''? $this->type : $type;
 446          $functionName   = 'image' . $type;
 447          $this->old_image = $this->imageHandle;
 448          $functionName($this->imageHandle, $filename) ;
 449          $this->imageHandle = $this->old_image;
 450          $this->resized = false;
 451      } // End save
 452  
 453  
 454      /**
 455       * Display image without saving and lose changes
 456       *
 457       * @param string type (JPG,PNG...);
 458       * @param int quality 75
 459       *
 460       * @return none
 461       */
 462      function display($type = '', $quality = 75)
 463      {
 464          if ($type != '') {
 465              $this->type = $type;
 466          }
 467          $functionName = 'Image' . $this->type;
 468          header('Content-type: image/' . strtolower($this->type));
 469          $functionName($this->imageHandle, '', $quality);
 470          $this->imageHandle = $this->old_image;
 471          $this->resized = false;
 472          ImageDestroy($this->old_image);
 473          $this->free();
 474      }
 475  
 476      /**
 477       * Destroy image handle
 478       *
 479       * @return none
 480       */
 481      function free()
 482      {
 483          if ($this->imageHandle){
 484              ImageDestroy($this->imageHandle);
 485          }
 486      }
 487  
 488  } // End class ImageGD
 489  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7