[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/gallery/resizers/ -> gallerygdresizer.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/gallery/resizers/galleryabstractresizer.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/gallery/resizers/gddetector.class.php" );
   5      
   6      define( "GD_RESIZER_NO_SMOOTHING_MODE", 0 );
   7      define( "GD_RESIZER_PHP_IMAGECOPYRESAMPLED", 1 );
   8      define( "GD_RESIZER_BILINEAR_MODE", 2 );
   9      define( "GD_RESIZER_BICUBIC_MODE", 3 );    
  10  
  11      /**
  12       * \ingroup Gallery_resizer
  13       *
  14       * Generates thumbnails using the built-in GD library functionality.
  15       *
  16       * Based off Shiege Iseng's (shiegege@yahoo.com) Resize Class found somewhere in
  17       * the net, but heavily modified specially concerning error situations.
  18       *
  19       */
  20      class GalleryGDResizer extends GalleryAbstractResizer 
  21      {
  22  
  23          var $img;
  24  
  25      	function GalleryGDResizer( $image, $outputMethod )
  26          {
  27              $this->GalleryAbstractResizer( $image, $outputMethod );
  28          }
  29  
  30          /**
  31           * @see GalleryResizer::generate
  32           */
  33          function generate( $outFile, $width, $height )
  34          {
  35              //
  36              // generate the thubmanil but check for errors every time
  37              //
  38              
  39              // also, check if GD is available because otherwise we would get
  40              // all sorts of nasty errors...
  41              if( !GdDetector::detectGd())
  42                  return false;
  43              
  44              if( !$this->thumbnail( $this->_image ))
  45                  return false;
  46  
  47              if( ($this->img["lebar"] < $width) && ($this->img["tinggi"] < $height) ) {
  48                  $this->img["lebar_thumb"] = $this->img["lebar"];
  49                  $this->img["tinggi_thumb"] = $this->img["tinggi"];
  50              }
  51              else {
  52                  $this->calcThumbFormat($width, $height);
  53              }
  54  
  55              if( !$this->save( $outFile ))
  56                  return false;
  57  
  58                  // depending on the default file creation settings in some hosts,
  59                  // files created may not be readable by the web server
  60              File::chMod( $outFile, 0644 );
  61  
  62              return $outFile;
  63          }
  64          
  65          /** 
  66           * @private
  67           */
  68          function thumbnail($imgfile)
  69          {
  70              //detect image format
  71              $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
  72              $this->img["format"]=strtoupper($this->img["format"]);
  73              if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
  74                  $this->img["format"]="JPEG";
  75                  $this->img["src"] = @ImageCreateFromJPEG ($imgfile);
  76              }
  77              elseif ($this->img["format"]=="PNG") {
  78                  $this->img["format"]="PNG";
  79                  $this->img["src"] = @ImageCreateFromPNG ($imgfile);
  80              }
  81              elseif ($this->img["format"]=="GIF") {
  82                  $this->img["format"]="GIF";
  83                  if( function_exists("imagecreatefromgif")) {
  84                      $this->img["src"] = @ImageCreateFromGIF ($imgfile);
  85                  }
  86                  else {
  87                      return false;
  88                  }
  89  
  90              }
  91              else {
  92                  // not a recognized format
  93                  throw( new Exception( "Trying to generate a thumbnail of an unsupported format!"));
  94                  //die();
  95              }
  96  
  97              // check for errors
  98              if( !$this->img["src"] )
  99                  return false;
 100  
 101              // if no errors, continue
 102              @$this->img["lebar"] = imagesx($this->img["src"]);
 103              @$this->img["tinggi"] = imagesy($this->img["src"]);
 104              //default quality jpeg
 105              $this->img["quality"]=85;
 106  
 107              return true;
 108          }
 109  
 110          /** 
 111           * @private
 112           */
 113          function size_height($size=100)
 114          {
 115              //height
 116              if( $this->img["lebar"] > $size ) {
 117                  $this->img["tinggi_thumb"]=$size;
 118                  @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
 119              }
 120              else {
 121                  $this->img["tinggi_thumb"]=$size;
 122                  $this->img["lebar_thumb"]=$this->img["lebar"]; 
 123              }
 124  
 125              return true;
 126          }
 127  
 128          /** 
 129           * @private
 130           */
 131          function size_width($size=100)
 132          {
 133              //width
 134              if( $this->img["tinggi"] > $size ) {
 135                  $this->img["lebar_thumb"]=$size;
 136                  @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
 137              }
 138              else {
 139                  $this->img["lebar_thumb"] = $size;
 140                  $this->img["tinggi_thumb"] = $this->img["tinggi"];
 141              }
 142  
 143              return true;
 144          }
 145          
 146          /** 
 147           * @private
 148           */
 149          function size_auto($size=100)
 150          {
 151              //size
 152              if ($this->img["lebar"]>=$this->img["tinggi"]) {
 153                  $this->img["lebar_thumb"]=$size;
 154                  @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
 155              }
 156              else {
 157                  $this->img["tinggi_thumb"]=$size;
 158                  @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
 159              }
 160  
 161              return true;
 162          }
 163  
 164          /** 
 165           * @private
 166           */
 167          function jpeg_quality($quality=75)
 168          {
 169              //jpeg quality
 170              $this->img["quality"]=$quality;
 171  
 172              return true;
 173          }
 174          
 175          /**
 176           * returns true if gd2 is available or false otherwise.
 177           * Based on a comment found in http://fi2.php.net/imagecreatetruecolor by aaron at aaron-wright dot com
 178           * (credit is due where it is due :)
 179           * 
 180           * @return true if GD2 is available or false otherwise
 181           */
 182          function isGD2Available()
 183          {
 184              // maybe the blog has been configured to use the gd1 routines no matter
 185              // if gd2 is detected or not... in that case, we don't go ahead and simply
 186              // say that gd2 is not available
 187              $config =& Config::getConfig();
 188              if( $config->getValue( "thumbnail_generator_force_use_gd1" )) {
 189                  return false;
 190              }
 191              
 192              // if not, we still check in case the user made a mistake...
 193              $testGD = get_extension_funcs("gd"); // Grab function list
 194              if ( !$testGD ) { 
 195                  return false;
 196              }
 197              if (in_array ("imagegd2",$testGD)) {
 198                  return true;
 199              }
 200              else { 
 201                  return false; 
 202              }
 203          }
 204  
 205          /**
 206           * resizes an image using several different techniques:
 207           *
 208           * PHP's own ImageCopyResamplated
 209           * Bi-linear filter (slower, but better quality than ImageCopyResampled)
 210           * Bi-Cubic filter (slowest, but offers the best quality)
 211           * PHP's own ImageCopyResized (fastest one, but offers no antialising or filter)
 212           *
 213           */
 214          function ImageResize($dst_img, &$src_img, $dst_x, $dst_y, $src_x, 
 215                                     $src_y, $dst_w, $dst_h, $src_w, $src_h, 
 216                                     $resample = GD_RESIZER_NO_SMOOTHING_MODE ) {
 217             $pxls = intval($src_w / $dst_w)-1;
 218  
 219             if( $dst_w == $dst_h ) {
 220                     $length = min($src_w, $src_h);
 221                     $src_x = intval( $src_w / 2 ) - intval( $length / 2 );
 222                     $src_y = intval( $src_h / 2 ) - intval( $length / 2 );
 223                     $src_w = $length;
 224                     $src_h = $length;
 225             }
 226                     
 227             if( $resample == GD_RESIZER_PHP_IMAGECOPYRESAMPLED ) {
 228                  imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);    
 229             }
 230             elseif( $resample == GD_RESIZER_BILINEAR_MODE  ) { //slow but better quality
 231                  ImageTrueColorToPalette( $src_img, false, 256 );
 232                  ImagePaletteCopy ($dst_img, $src_img);
 233                  $rX = $src_w / $dst_w;
 234                  $rY = $src_h / $dst_h;
 235                  $nY = 0;
 236                  for ($y=$src_y; $y<$dst_h; $y++) {
 237                      $oY = $nY;
 238                      $nY = intval(($y + 1) * $rY+.5);
 239                      $nX = 0;
 240                      for ($x=$src_x; $x<$dst_w; $x++) {
 241                           $r = $g = $b = $a = 0;
 242                           $oX = $nX;
 243                           $nX = intval(($x + 1) * $rX+.5);
 244                           $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $nX, $nY));
 245                           $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++;
 246                           $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $nX-$pxls, $nY-$pxls));
 247                           $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++;
 248                           //you can add more pixels here! eg "$nX, $nY-$pxls" or "$nX-$pxls, $nY"
 249                           ImageSetPixel ($dst_img, ($x+$dst_x-$src_x), ($y+$dst_y-$src_y), ImageColorClosest ($dst_img, $r/$a, $g/$a, $b/$a));
 250                      }
 251                  }
 252             } 
 253             elseif ( $resample == GD_RESIZER_BICUBIC_MODE ) { // veeeeeery slow but better quality
 254                       ImagePaletteCopy ($dst_img, $src_img);
 255                       $rX = $src_w / $dst_w;
 256                       $rY = $src_h / $dst_h;
 257                       $nY = 0;
 258                       for ($y=$src_y; $y<$dst_h; $y++) {
 259                         $oY = $nY;
 260                         $nY = intval(($y + 1) * $rY+.5);
 261                         $nX = 0;
 262                         for ($x=$src_x; $x<$dst_w; $x++) {
 263                           $r = $g = $b = $a = 0;
 264                           $oX = $nX;
 265                           $nX = intval(($x + 1) * $rX+.5);
 266                           for ($i=$nY; --$i>=$oY;) {
 267                             for ($j=$nX; --$j>=$oX;) {
 268                               $c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $j, $i));
 269                               $r += $c['red'];
 270                               $g += $c['green'];
 271                               $b += $c['blue'];
 272                               $a++;
 273                             }
 274                           }
 275                           ImageSetPixel ($dst_img, ($x+$dst_x-$src_x), ($y+$dst_y-$src_y), ImageColorClosest ($dst_img, $r/$a, $g/$a, $b/$a));
 276                         }
 277                       }
 278             } 
 279             else {
 280               $dst_w++; $dst_h++; //->no black border
 281               imagecopyresized($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 282             }
 283          }
 284  
 285          /** 
 286           * @private
 287           */
 288          function save( $save = "" )
 289          {
 290              $fileParts = explode( ".", $save );
 291              $fileNoExt = implode( ".", $fileParts );
 292              $fileExt = strtolower($fileParts[count($fileParts)-1]);
 293          
 294              //if( function_exists("imagecreatetruecolor")) {
 295              if( $this->isGD2Available()) {
 296                  $this->img["des"] = @ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
 297              }
 298              else {
 299                  $this->img["des"] = @ImageCreate($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
 300              }
 301  
 302              // check for errors and stop if any, or continue otherwise
 303              if( $this->img["des"] == "" )
 304                  return false;
 305                  
 306              $config =& Config::getConfig();
 307              if( $this->isGD2Available())
 308                  $resizeMode = $config->getValue( "thumbnail_generator_use_smoothing_algorithm", GD_RESIZER_NO_SMOOTHING_MODE );
 309              else
 310                  $resizeMode = GD_RESIZER_NO_SMOOTHING_MODE;
 311              
 312              // resize the image using the mode chosen above
 313              $this->ImageResize($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"], $resizeMode ); 
 314  
 315              // format for thumbnails is the same as the image
 316              //if( $this->_outputMethod == THUMBNAIL_OUTPUT_FORMAT_SAME_AS_IMAGE || $this->_outputMethod == "" ) {
 317                  if ($fileExt=="jpg" || $fileExt=="jpeg") {
 318                      $result = @imageJPEG($this->img["des"],"$save",$this->img["quality"]);
 319                  }
 320                  elseif ($fileExt=="png") {
 321                      $result = @imagePNG($this->img["des"],"$save");
 322                  }
 323                  elseif ($fileExt=="gif") {
 324                      if( function_exists("imagegif")) {
 325                          $result = @imageGIF($this->img["des"],"$save");
 326                      }
 327                      else {
 328                          $result = false;
 329                      }
 330                  }
 331  
 332              return $result;
 333          }
 334  
 335          function calcThumbFormat($width, $height){
 336              $ratioimg = (float) $this->img["lebar"] / (float) $this->img["tinggi"];
 337              $ratiothumb=(float) $width / (float) $height;
 338  
 339              if ($ratioimg == $ratiothumb || $ratiothumb == (float) 1){
 340                  $this->img["lebar_thumb"]=$width;
 341                  $this->img["tinggi_thumb"]=$height;
 342              } elseif ($ratioimg > $ratiothumb){
 343                  $length = max($width, $height);
 344                  $this->img["lebar_thumb"]=$length;
 345                  $this->img["tinggi_thumb"]=$length*((float)1/$ratioimg);
 346              } elseif ($ratioimg < $ratiothumb){
 347                  $length = max($width, $height);
 348                  $this->img["tinggi_thumb"]=$length;
 349                  $this->img["lebar_thumb"]=$length*$ratioimg;
 350              }
 351          }
 352          
 353      }
 354  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics