[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/lib/ezimage/classes/ -> ezimagegdhandler.php (source)

   1  <?php
   2  //
   3  // Definition of eZImageGDHandler class
   4  //
   5  // Created on: <16-Oct-2003 14:22:43 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezimagegdhandler.php
  30  */
  31  
  32  /*!
  33    \class eZImageGDHandler ezimagegdhandler.php
  34    \ingroup eZImage
  35    \brief The class eZImageGDHandler does
  36  
  37    A geometry array has the following entries.
  38    - x - The x position
  39    - y - The y position
  40    - width - The width
  41    - height - The height
  42  */
  43  
  44  include_once ( 'lib/ezimage/classes/ezimagehandler.php' );
  45  
  46  class eZImageGDHandler extends eZImageHandler
  47  {
  48      /*!
  49       Constructor
  50      */
  51      function eZImageGDHandler( $handlerName, $isGloballyEnabled,
  52                                 $outputRewriteType = EZ_IMAGE_HANDLER_REPLACE_SUFFIX,
  53                                 $conversionRules = false )
  54      {
  55          $supportedInputMIMETypes = array();
  56          $supportedOutputMIMETypes = array();
  57          $this->InputMap = array();
  58          $this->OutputMap = array();
  59          $this->OutputQualityMap = array();
  60          $isEnabled = false;
  61          if ( function_exists( "imagetypes" ) )
  62          {
  63              $gdFunctions = array( array( 'mimetype' => 'image/gif',
  64                                           'input' => 'imagecreatefromgif',
  65                                           'output' => 'imagegif' ),
  66                                    array( 'mimetype' => 'image/vnd.wap.wbmp',
  67                                           'input' => 'imagecreatefromwbmp',
  68                                           'output' => 'imagewbmp' ),
  69                                    array( 'mimetype' => 'image/png',
  70                                           'input' => 'imagecreatefrompng',
  71                                           'output' => 'imagepng' ),
  72                                    array( 'mimetype' => 'image/jpeg',
  73                                           'input' => 'imagecreatefromjpeg',
  74                                           'output' => 'imagejpeg',
  75                                           'qualityparameter' => true ) );
  76              foreach ( $gdFunctions as $gdFunction )
  77              {
  78                  $inputFunction = $gdFunction['input'];
  79                  $outputFunction = $gdFunction['output'];
  80                  if ( function_exists( $inputFunction ) or function_exists( $outputFunction ) )
  81                  {
  82                      $isEnabled = true;
  83                      $mimeType = $gdFunction['mimetype'];
  84                      if ( function_exists( $inputFunction ) )
  85                      {
  86                          $supportedInputMIMETypes[] = $mimeType;
  87                          $this->InputMap[$mimeType] = $inputFunction;
  88                      }
  89                      if ( function_exists( $outputFunction ) )
  90                      {
  91                          $this->OutputMap[$mimeType] = $outputFunction;
  92                          $supportedOutputMIMETypes[] = $mimeType;
  93                      }
  94                      if ( isset( $gdFunction['qualityparameter'] ) )
  95                          $this->OutputQualityMap[$mimeType] = $gdFunction['qualityparameter'];
  96                      else
  97                          $this->OutputQualityMap[$mimeType] = false;
  98                  }
  99              }
 100          }
 101          if ( !$isGloballyEnabled )
 102              $isEnabled = false;
 103          $this->FilterFunctionMap = array( 'geometry/scale' => 'scaleImage',
 104                                            'geometry/scalewidth' => 'scaleImageWidth',
 105                                            'geometry/scaleheight' => 'scaleImageHeight',
 106                                            'geometry/scaledownonly' => 'scaleImageDownOnly',
 107                                            'geometry/scalewidthdownonly' => 'scaleImageWidthDownOnly',
 108                                            'geometry/scaleheightdownonly' => 'scaleImageHeightDownOnly',
 109                                            'geometry/scaleexact' => 'scaleImageExact',
 110                                            'geometry/scalepercent' => 'scaleImagePercent',
 111                                            'geometry/crop' => 'cropImage',
 112                                            'colorspace/gray' => 'setImageColorspaceGray',
 113                                            'luminance' => 'setImageLuminance',
 114                                            'luminance/gray' => 'setImageLuminanceNamed',
 115                                            'luminance/sepia' => 'setImageLuminanceNamed',
 116                                            'color/monochrome' => 'setImageColorThresholdName',
 117                                            'border' => 'createImageBorder',
 118                                            'border/color' => 'setImageBorderColor',
 119                                            'border/width' => 'setImageBorderWidth' );
 120          $this->LuminanceColorScales = array( 'luminance/gray' => array( 1.0, 1.0, 1.0 ),
 121                                               'luminance/sepia' => array( 1.0, 0.89, 0.74 ) );
 122          $this->ThresholdList = array( 'color/monochrome' => array( array( 'threshold' => 127,
 123                                                                            'rgb' => array( 0, 0, 0 ) ),
 124                                                                     array( 'threshold' => 255,
 125                                                                            'rgb' => array( 255, 255, 255 ) ) ) );
 126  
 127          $filters = array();
 128          foreach ( $this->FilterFunctionMap as $filterName => $filterFunction )
 129          {
 130              $filters[] = array( 'name' => $filterName );
 131          }
 132          $this->eZImageHandler( $handlerName, $isEnabled,
 133                                 $outputRewriteType,
 134                                 $supportedInputMIMETypes, $supportedOutputMIMETypes,
 135                                 $conversionRules, $filters );
 136      }
 137  
 138      /*!
 139       Creates the shell string and runs the executable.
 140      */
 141      function convert( &$manager, $sourceMimeData, &$destinationMimeData, $filters = false )
 142      {
 143          $sourceMimeType = $sourceMimeData['name'];
 144          $destinationMimeType = $destinationMimeData['name'];
 145          if ( !isset( $this->InputMap[$sourceMimeType] ) )
 146          {
 147              eZDebug::writeError( "MIME-Type $sourceMimeType is not supported as input by GD converter",
 148                                   'eZImageGDHandler::convert' );
 149              return false;
 150          }
 151          if ( !isset( $this->OutputMap[$destinationMimeType] ) )
 152          {
 153              eZDebug::writeError( "MIME-Type $destinationMimeType is not supported as output by GD converter",
 154                                   'eZImageGDHandler::convert' );
 155              return false;
 156          }
 157  
 158          $inputFunction = $this->InputMap[$sourceMimeType];
 159          $outputFunction = $this->OutputMap[$destinationMimeType];
 160          $outputQualityParameter = $this->OutputQualityMap[$destinationMimeType];
 161          $inputFile = $sourceMimeData['url'];
 162          $outputFile = $destinationMimeData['url'];
 163  
 164          if ( !file_exists( $inputFile ) )
 165          {
 166              eZDebug::writeError( "Source image $inputFile does not exist, cannot convert",
 167                                   'eZImageGDHandler::convert' );
 168              return false;
 169          }
 170          $inputImage = $inputFunction( $inputFile );
 171  
 172          $currentImage =& $inputImage;
 173  
 174          $filterVariables = array( 'border-color' => array( 127, 127, 127 ),
 175                                    'border-size' => array( 0, 0 ) );
 176  
 177          if ( $filters !== false )
 178          {
 179              foreach ( $filters as $filterData )
 180              {
 181                  $filterName = $filterData['name'];
 182                  if ( isset( $this->FilterFunctionMap[$filterName] ) )
 183                  {
 184                      $filterFunction = $this->FilterFunctionMap[$filterName];
 185                      $filteredImage =& $this->$filterFunction( $currentImage, $filterData, $filterVariables, $sourceMimeData, $destinationMimeData );
 186                      if ( $filteredImage !== false )
 187                      {
 188                          if ( $filteredImage != $currentImage )
 189                          {
 190                              ImageDestroy( $currentImage );
 191                          }
 192                          $currentImage =& $filteredImage;
 193                      }
 194                  }
 195              }
 196          }
 197  
 198          $outputImage =& $currentImage;
 199  
 200          if ( $outputImage )
 201          {
 202              $outputQuality = false;
 203              if ( $outputQualityParameter )
 204                  $outputQuality = $manager->qualityValue( $destinationMimeType );
 205              if ( $outputQuality !== false )
 206              {
 207                  $returnCode = $outputFunction( $outputImage, $outputFile, $outputQuality );
 208              }
 209              else
 210              {
 211                  $returnCode = $outputFunction( $outputImage, $outputFile );
 212              }
 213  
 214              ImageDestroy( $outputImage );
 215          }
 216          else
 217              $returnCode = false;
 218  
 219          if ( $returnCode )
 220          {
 221              if ( !file_exists( $destinationMimeData['url'] ) )
 222              {
 223                  eZDebug::writeError( "Unknown destination file: " . $destinationMimeData['url'], "eZImageGDHandler(" . $this->HandlerName . ")" );
 224                  return false;
 225              }
 226              $this->changeFilePermissions( $destinationMimeData['url'] );
 227              return true;
 228          }
 229          else
 230          {
 231              eZDebug::writeWarning( "Failed converting $inputFile ($sourceMimeType) to $outputFile ($destinationMimeType)", 'eZImageGDHandler::convert' );
 232              return false;
 233          }
 234      }
 235  
 236      function setImageBorderColor( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 237      {
 238          $filterVariables['border-color'] = $filterData['data'];
 239          return false;
 240      }
 241  
 242      function setImageBorderWidth( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 243      {
 244          $filterVariables['border-size'] = array( $filterData['data'][0], $filterData['data'][0] );
 245          return $this->createImageBorder( $imageObject, $filterData, $filterVariables, $sourceMimeData, $destinationMimeData );
 246      }
 247  
 248      function &setImageBorder( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 249      {
 250          $filterVariables['border-size'] = array( $filterData['data'][0], $filterData['data'][1] );
 251          return $this->createImageBorder( $imageObject, $filterData, $filterVariables, $sourceMimeData, $destinationMimeData );
 252      }
 253  
 254      function &createImageBorder( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 255      {
 256          $width = ImageSX( $imageObject );
 257          $height = ImageSY( $imageObject );
 258          $borderWidth = $filterVariables['border-size'][0];
 259          $borderHeight = $filterVariables['border-size'][1];
 260          $borderColor = $filterVariables['border-color'];
 261          $newWidth = $width + $borderWidth*2;
 262          $newHeight = $height + $borderHeight*2;
 263  
 264          $temporaryImageObject =& $this->imageCopy( $imageObject,
 265                                                     $this->createGeometry( $newWidth, $newHeight, $borderWidth, $borderHeight ),
 266                                                     $this->createGeometry( $width, $height, 0, 0 ),
 267                                                     $sourceMimeData, $destinationMimeData );
 268          $color = ImageColorAllocate( $temporaryImageObject, $borderColor[0], $borderColor[1], $borderColor[2] );
 269          ImageFilledRectangle( $temporaryImageObject, 0, 0, $newWidth, $borderHeight, $color );
 270          ImageFilledRectangle( $temporaryImageObject, $newWidth - $borderWidth, 0, $newWidth, $newHeight, $color );
 271          ImageFilledRectangle( $temporaryImageObject, 0, $newHeight - $borderHeight, $newWidth, $newHeight, $color );
 272          ImageFilledRectangle( $temporaryImageObject, 0, 0, $borderWidth, $newHeight, $color );
 273          return $temporaryImageObject;
 274      }
 275  
 276      /*!
 277       Converts the image to grayscale.
 278      */
 279      function &setImageColorspaceGray( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 280      {
 281          $colorScale = array( 1.0, 1.0, 1.0 );
 282          return $this->setImageLuminanceColorScale( $imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 283                                                     $colorScale );
 284      }
 285  
 286      /*!
 287       Changes the colors of the image based on the luminance.
 288       The new scale for the colors are taken from the filter parameters, the parameters must contain three values.
 289      */
 290      function &setImageLuminance( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 291      {
 292          $colorScale = $filterData['data'];
 293          return $this->setImageLuminanceColorScale( $imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 294                                                     $colorScale );
 295      }
 296  
 297      /*!
 298       Changes the colors of the image based on the luminance.
 299       The new scale for the colors are based on the name of the filters.
 300      */
 301      function &setImageLuminanceNamed( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 302      {
 303          if ( isset( $this->LuminanceColorScales[$filterData['name']] ) )
 304          {
 305              $colorScale = $this->LuminanceColorScales[$filterData['name']];
 306          }
 307          else
 308          {
 309              eZDebug::writeDebug( "No luminance scale named " . $filterData['name'] . ", applying gray scales",
 310                                   'eZImageGDHandler::setImageLuminanceName' );
 311              $colorScale = array( 1.0, 1.0, 1.0 );
 312          }
 313          return $this->setImageLuminanceColorScale( $imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 314                                                     $colorScale );
 315      }
 316  
 317      /*!
 318       Changes the colors of the image based on the luminance.
 319       \param $colorScale is an array with three float elements in range 0 to 1 that define the new color scale.
 320      */
 321      function &setImageLuminanceColorScale( &$imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 322                                             $colorScale )
 323      {
 324          $white = ImageColorAllocate( $imageObject, 255, 255, 255 );
 325          $black = ImageColorAllocate( $imageObject, 0, 0, 0 );
 326  
 327          $rmod = $colorScale[0];
 328          $gmod = $colorScale[1];
 329          $bmod = $colorScale[2];
 330  
 331          $width = ImageSX( $imageObject );
 332          $height = ImageSY( $imageObject );
 333          for ( $y = 0; $y < $height; ++$y )
 334          {
 335              for ( $x = 0; $x < $width; ++$x )
 336              {
 337                  $rgb = ImageColorAt( $imageObject, $x, $y );
 338  
 339                  $r = ( $rgb >> 16 ) & 0xff;
 340                  $g = ( $rgb >> 8 ) & 0x0ff;
 341                  $b = ( $rgb ) & 0x0ff;
 342  
 343                  $luminance = ( $r * 0.3 ) + ( $g * 0.59 ) + ( $b * 0.11 );
 344  
 345                  $r = $luminance * $rmod;
 346                  $g = $luminance * $gmod;
 347                  $b = $luminance * $bmod;
 348  
 349                  $color = ImageColorAllocate( $imageObject, $r, $g, $b );
 350  
 351                  ImageSetPixel( $imageObject, $x, $y, $color );
 352              }
 353          }
 354          return $imageObject;
 355      }
 356  
 357      /*!
 358       Changes the colors of the image based on threshold values.
 359       The threshold values are based on the filter name.
 360      */
 361      function &setImageColorThresholdName( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 362      {
 363          if ( isset( $this->ThresholdList[$filterData['name']] ) )
 364          {
 365              $thresholdList = $this->ThresholdList[$filterData['name']];
 366          }
 367          else
 368          {
 369              eZDebug::writeDebug( "No threshold values named " . $filterData['name'] . ", applying black/white (monochrome) threshold",
 370                                   'eZImageGDHandler::setImageLuminanceName' );
 371              $thresholdList = array( array( 'threshold' => 127,
 372                                             'rgb' => array( 0, 0, 0 ) ),
 373                                      array( 'threshold' => 255,
 374                                             'rgb' => array( 255, 255, 255 ) ) );
 375          }
 376          return $this->setImageColorThreshold( $imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 377                                                $thresholdList );
 378      }
 379  
 380      /*!
 381       Changes the colors of the image based on threshold values. The luminance will be calculated and if it is
 382       in a threshold range it will use the specified color for the range.
 383      */
 384      function &setImageColorThreshold( &$imageObject, $filterData, $sourceMimeData, $destinationMimeData,
 385                                        $thresholdList )
 386      {
 387          foreach ( array_keys( $thresholdList ) as $thresholdKey )
 388          {
 389              $thresholdItem =& $thresholdList[$thresholdKey];
 390              $thresholdItem['color'] = ImageColorAllocate( $imageObject, $thresholdItem['rgb'][0], $thresholdItem['rgb'][1], $thresholdItem['rgb'][2] );
 391          }
 392          $defaultColor = $thresholdList[count( $thresholdList ) - 1]['color'];
 393  
 394          $width = ImageSX( $imageObject );
 395          $height = ImageSY( $imageObject );
 396          for ( $y = 0; $y < $height; ++$y )
 397          {
 398              for ( $x = 0; $x < $width; ++$x )
 399              {
 400                  $rgb = ImageColorAt( $imageObject, $x, $y );
 401  
 402                  $r = ( $rgb >> 16 ) & 0xff;
 403                  $g = ( $rgb >> 8 ) & 0x0ff;
 404                  $b = ( $rgb ) & 0x0ff;
 405  
 406                  $luminance = ( $r * 0.3 ) + ( $g * 0.59 ) + ( $b * 0.11 );
 407  
 408                  $color = false;
 409                  foreach ( $thresholdList as $thresholdItem )
 410                  {
 411                      if ( $luminance <= $thresholdItem['threshold'] )
 412                      {
 413                          $color = $thresholdItem['color'];
 414                          break;
 415                      }
 416                  }
 417                  if ( $color === false )
 418                      $color = $defaultColor;
 419  
 420                  ImageSetPixel( $imageObject, $x, $y, $color );
 421              }
 422          }
 423          return $imageObject;
 424      }
 425  
 426      /*!
 427        Crops a portion of the image from the filter parameters.
 428      */
 429      function &cropImage( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 430      {
 431          $width = $filterData['data'][0];
 432          $height = $filterData['data'][1];
 433          $x = $filterData['data'][2];
 434          $y = $filterData['data'][3];
 435          $imageWidth = ImageSX( $imageObject );
 436          $imageHeight = ImageSY( $imageObject );
 437          $width = max( min( $width, $imageWidth - $x ), 0 );
 438          $height = max( min( $height, $imageHeight - $y ), 0 );
 439          $destinationGeometry = $this->createGeometry( $width, $height, 0, 0 );
 440          $geometry = $this->createGeometry( $width, $height, $x, $y );
 441          return $this->imageCopy( $imageObject,
 442                                   $destinationGeometry,
 443                                   $geometry,
 444                                   $sourceMimeData, $destinationMimeData );
 445      }
 446  
 447      /*!
 448       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 449       This means that image will not be exactly the image size.
 450       \sa scaleImageExact
 451      */
 452      function &scaleImage( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 453      {
 454          $geometry = $this->calculateScaledAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 455                                                            $filterData['data'][0], $filterData['data'][1], true );
 456          return $this->scaleImageCopy( $imageObject,
 457                                        $geometry,
 458                                        $sourceMimeData, $destinationMimeData );
 459      }
 460  
 461      /*!
 462       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 463       This means that image will not be exactly the image size.
 464       \note The image will not be scaled if the source size is smaller than the destination size.
 465       \sa scaleImageExact
 466      */
 467      function &scaleImageDownOnly( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 468      {
 469          $geometry = $this->calculateScaledAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 470                                                            $filterData['data'][0], $filterData['data'][1], false );
 471          $scaleDownOnly =& $this->scaleImageCopy( $imageObject,
 472                                                   $geometry,
 473                                                   $sourceMimeData, $destinationMimeData );
 474  
 475          return $scaleDownOnly;
 476      }
 477  
 478      /*!
 479       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 480       This means that image will not be exactly the image size.
 481       \sa scaleImageExact
 482      */
 483      function &scaleImageWidth( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 484      {
 485          $geometry = $this->calculateFixedWidthAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 486                                                                $filterData['data'][0], true );
 487          return $this->scaleImageCopy( $imageObject,
 488                                        $geometry,
 489                                        $sourceMimeData, $destinationMimeData );
 490      }
 491  
 492      /*!
 493       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 494       This means that image will not be exactly the image size.
 495       \sa scaleImageExact
 496      */
 497      function &scaleImageHeight( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 498      {
 499          $geometry = $this->calculateFixedHeightAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 500                                                                 $filterData['data'][0], true );
 501          return $this->scaleImageCopy( $imageObject,
 502                                        $geometry,
 503                                        $sourceMimeData, $destinationMimeData );
 504      }
 505  
 506      /*!
 507       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 508       This means that image will not be exactly the image size.
 509       \note The image will not be scaled if the source size is smaller than the destination size.
 510       \sa scaleImageExact
 511      */
 512      function &scaleImageWidthDownOnly( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 513      {
 514          $geometry = $this->calculateFixedWidthAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 515                                                                $filterData['data'][0], false );
 516          return $this->scaleImageCopy( $imageObject,
 517                                        $geometry,
 518                                        $sourceMimeData, $destinationMimeData );
 519      }
 520  
 521      /*!
 522       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ration maintained.
 523       This means that image will not be exactly the image size.
 524       \note The image will not be scaled if the source size is smaller than the destination size.
 525       \sa scaleImageExact
 526      */
 527      function &scaleImageHeightDownOnly( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 528      {
 529          $geometry = $this->calculateFixedHeightAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 530                                                                 $filterData['data'][0], false );
 531          return $this->scaleImageCopy( $imageObject,
 532                                        $geometry,
 533                                        $sourceMimeData, $destinationMimeData );
 534      }
 535  
 536      /*!
 537       Scales the image \a $imageObject to the size specified in \a $filterData without caring about aspect ratio.
 538      */
 539      function &scaleImageExact( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 540      {
 541          return $this->scaleImageCopy( $imageObject,
 542                                        $this->createGeometry( $filterData['data'][0], $filterData['data'][1] ),
 543                                        $sourceMimeData, $destinationMimeData );
 544      }
 545  
 546      /*!
 547       Scales the image \a $imageObject to the size specified in \a $filterData with aspect ratio maintained.
 548      */
 549      function &scaleImagePercent( &$imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
 550      {
 551          $geometry = $this->calculateScaledPercentAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
 552                                                                   $filterData['data'][0] / 100.0, $filterData['data'][1] / 100.0, true );
 553          return $this->scaleImageCopy( $imageObject,
 554                                        $geometry,
 555                                        $sourceMimeData, $destinationMimeData );
 556      }
 557  
 558      /*!
 559       Calculates the geometry for the scaled image while maintaining the aspect ratio.
 560       \param $allowUpScale If this is true images will be scaled up as well, if not they will keep their source size.
 561       \return a geometry array.
 562       \sa createGeometry
 563      */
 564      function calculateScaledAspectGeometry( $sourceWidth, $sourceHeight,
 565                                              $destinationWidth, $destinationHeight,
 566                                              $allowUpScale )
 567      {
 568          $widthScale = $sourceWidth / $destinationWidth;
 569          $heightScale = $sourceHeight / $destinationHeight;
 570  
 571          $scale = $heightScale;
 572          if ( $heightScale != $widthScale )
 573              $scale = max( $heightScale, $widthScale );
 574  
 575          if ( $scale < 1.0 and !$allowUpScale )
 576          {
 577              $destinationWidth = $sourceWidth;
 578              $destinationHeight = $sourceHeight;
 579          }
 580          else
 581          {
 582              $destinationWidth = (int) ( $sourceWidth / $scale );
 583              $destinationHeight = (int) ( $sourceHeight / $scale );
 584          }
 585          return $this->createGeometry( $destinationWidth, $destinationHeight );
 586      }
 587  
 588      /*!
 589       Calculates the geometry for the scaled image in terms of percent while maintaining the aspect ratio.
 590       \param $allowUpScale If this is true images will be scaled up as well, if not they will keep their source size.
 591       \note Percentage must be given as a float value, e.g. 50% is 0.5 and 200% is 2.0
 592       \return a geometry array.
 593       \sa createGeometry
 594      */
 595      function calculateScaledPercentAspectGeometry( $sourceWidth, $sourceHeight,
 596                                                     $destinationWidthPercent, $destinationHeightPercent,
 597                                                     $allowUpScale )
 598      {
 599          $destinationWidth = $sourceWidth * $destinationWidthPercent;
 600          $destinationHeight = $sourceHeight * $destinationHeightPercent;
 601          return $this->calculateScaledAspectGeometry( $sourceWidth, $sourceHeight,
 602                                                       $destinationWidth, $destinationHeight,
 603                                                       $allowUpScale );
 604      }
 605  
 606      /*!
 607       Calculates the geometry for the scaled image with a fixed width while maintaining the aspect ratio.
 608       \param $allowUpScale If this is true images will be scaled up as well, if not they will keep their source size.
 609       \return a geometry array.
 610       \sa createGeometry
 611      */
 612      function calculateFixedWidthAspectGeometry( $sourceWidth, $sourceHeight,
 613                                                  $destinationWidth,
 614                                                  $allowUpScale )
 615      {
 616          $scale = $sourceWidth / $destinationWidth;
 617  
 618          if ( $scale < 1.0 and !$allowUpScale )
 619          {
 620              $destinationWidth = $sourceWidth;
 621              $destinationHeight = $sourceHeight;
 622          }
 623          else
 624          {
 625              $destinationWidth = (int) ( $sourceWidth / $scale );
 626              $destinationHeight = (int) ( $sourceHeight / $scale );
 627          }
 628          return $this->createGeometry( $destinationWidth, $destinationHeight );
 629      }
 630  
 631      /*!
 632       Calculates the geometry for the scaled image with a fixed height while maintaining the aspect ratio.
 633       \param $allowUpScale If this is true images will be scaled up as well, if not they will keep their source size.
 634       \return a geometry array.
 635       \sa createGeometry
 636      */
 637      function calculateFixedHeightAspectGeometry( $sourceWidth, $sourceHeight,
 638                                                   $destinationHeight,
 639                                                   $allowUpScale )
 640      {
 641          $scale = $sourceHeight / $destinationHeight;
 642  
 643          if ( $scale < 1.0 and !$allowUpScale )
 644          {
 645              $destinationWidth = $sourceWidth;
 646              $destinationHeight = $sourceHeight;
 647          }
 648          else
 649          {
 650              $destinationWidth = (int) ( $sourceWidth / $scale );
 651              $destinationHeight = (int) ( $sourceHeight / $scale );
 652          }
 653          return $this->createGeometry( $destinationWidth, $destinationHeight );
 654      }
 655  
 656      /*!
 657       Scales the image \a $imageObject to the size specified in \a $filterData.
 658      */
 659      function &scaleImageCopy( &$imageObject,
 660                                $geometry,
 661                                $sourceMimeData, $destinationMimeData )
 662      {
 663          $destinationWidth = $geometry['width'];
 664          $destinationHeight = $geometry['height'];
 665          $sourceWidth = ImageSX( $imageObject );
 666          $sourceHeight = ImageSY( $imageObject );
 667  
 668          $temporaryImageObject = $this->imageCreate( $destinationWidth, $destinationHeight, eZImageGDHandler::isImageTrueColor( $imageObject, $sourceMimeData ) );
 669          ImageCopyResampled( $temporaryImageObject, $imageObject,
 670                              0, 0, 0, 0,
 671                              $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight );
 672          return $temporaryImageObject;
 673      }
 674  
 675      /*!
 676        Copies a portion of the source image \a $imageObject to a new image.
 677      */
 678      function &imageCopy( &$imageObject, $destinationGeometry, $sourceGeometry,
 679                           $sourceMimeData, $destinationMimeData )
 680      {
 681          $destinationWidth = $destinationGeometry['width'];
 682          $destinationHeight = $destinationGeometry['height'];
 683  
 684          $temporaryImageObject = $this->imageCreate( $destinationWidth, $destinationHeight, eZImageGDHandler::isImageTrueColor( $imageObject, $sourceMimeData ) );
 685          ImageCopy( $temporaryImageObject, $imageObject,
 686                     $destinationGeometry['x'], $destinationGeometry['y'],
 687                     $sourceGeometry['x'], $sourceGeometry['y'], $sourceGeometry['width'], $sourceGeometry['height'] );
 688          return $temporaryImageObject;
 689      }
 690  
 691      /*!
 692       \static
 693       \return \c true if the image object \a $imageObject is in true color format.
 694      */
 695      function isImageTrueColor( &$imageObject, $mimeData )
 696      {
 697          if ( eZSys::isPHPVersionSufficient( array( 4, 3, 2 ) ) )
 698              return ImageIsTrueColor( $imageObject );
 699          $nonTrueColorMimeTypes = array( 'image/gif' );
 700          if ( in_array( $sourceMimeData['name'], $nonTrueColorMimeTypes ) )
 701              return false;
 702          return true;
 703      }
 704  
 705      /*!
 706       Creates a new GD image and returns it.
 707       \param $isTrueColor determines if a true color image is created, if false an indexed image is created.
 708      */
 709      function imageCreate( $width, $height, $isTrueColor = true )
 710      {
 711          if ( $isTrueColor )
 712              return ImageCreateTrueColor( $width, $height );
 713          else
 714              return ImageCreate( $width, $height );
 715      }
 716  
 717      /*!
 718       Creates a geometry array with width \a $width, height \a $height, x position \a $x and y position \a $y and returns it.
 719      */
 720      function createGeometry( $width, $height, $x = 0, $y = 0 )
 721      {
 722          return array( 'x' => $x,
 723                        'y' => $y,
 724                        'width' => $width,
 725                        'height' => $height );
 726      }
 727  
 728      /*!
 729       Creates a new image handler for shell executable from INI settings.
 730       The INI settings are read from ini file \a $iniFilename and group \a $iniGroup.
 731       If \a $iniFilename is not supplied \c image.ini is used.
 732      */
 733      function &createFromINI( $iniGroup, $iniFilename = false )
 734      {
 735          if ( !$iniFilename )
 736              $iniFilename = 'image.ini';
 737  
 738          $handler = false;
 739          include_once ( 'lib/ezutils/classes/ezini.php' );
 740          $ini =& eZINI::instance( $iniFilename );
 741          if ( !$ini )
 742          {
 743              eZDebug::writeError( "Failed loading ini file $iniFilename",
 744                                   'eZImageGDHandler::createFromINI' );
 745              return $handler;
 746          }
 747  
 748          if ( $ini->hasGroup( $iniGroup ) )
 749          {
 750              $name = $iniGroup;
 751              if ( $ini->hasVariable( $iniGroup, 'Name' ) )
 752                  $name = $ini->variable( $iniGroup, 'Name' );
 753              $conversionRules = false;
 754              if ( $ini->hasVariable( $iniGroup, 'ConversionRules' ) )
 755              {
 756                  $conversionRules = array();
 757                  $rules = $ini->variable( $iniGroup, 'ConversionRules' );
 758                  foreach ( $rules as $ruleString )
 759                  {
 760                      $ruleItems = explode( ';', $ruleString );
 761                      if ( count( $ruleItems ) >= 2 )
 762                      {
 763                          $conversionRules[] = array( 'from' => $ruleItems[0],
 764                                                      'to' => $ruleItems[1] );
 765                      }
 766                  }
 767              }
 768              $isEnabled = $ini->variable( $iniGroup, 'IsEnabled' ) == 'true';
 769              $outputRewriteType = EZ_IMAGE_HANDLER_REPLACE_SUFFIX;
 770              $handler = new eZImageGDHandler( $name, $isEnabled,
 771                                               $outputRewriteType,
 772                                               $conversionRules );
 773              return $handler;
 774          }
 775          return $handler;
 776      }
 777  
 778      /// \privatesection
 779      var $Path;
 780      var $Executable;
 781      var $PreParameters;
 782      var $PostParameters;
 783  }
 784  
 785  class eZImageGDFactory extends eZImageFactory
 786  {
 787      /*!
 788       Initializes the factory with the name \c 'shell'
 789      */
 790      function eZImageGDFactory()
 791      {
 792          $this->eZImageFactory( 'gd' );
 793      }
 794  
 795      /*!
 796       \reimp
 797       Creates eZImageGDHandler objects and returns them.
 798      */
 799      function &produceFromINI( $iniGroup, $iniFilename = false )
 800      {
 801          $convertHandler =& eZImageGDHandler::createFromINI( $iniGroup, $iniFilename );
 802          return $convertHandler;
 803      }
 804  }
 805  
 806  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7