[ 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/ -> galleryresizer.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup Gallery_resizer
   5       *
   6       * Please use the proxy class GalleryResizer that should be used to generate thumbnails, instead of using directly
   7       * classes such as GalleryGdResizer or GalleryImageMagickResizer. This class will check from the 
   8       * configuration the values of the parameters <b>thumbnail_method</b> and <b>thumbnail_format</b>
   9       * and load the appropiate thumbnail generator class.
  10       *
  11       * Example usage:
  12       *
  13       * <pre>
  14       *  $generator = new GalleryResizer( "/tmp/image.jpg" );
  15       *  if( $generator->generate( "/tmp/image_thum.jpg", 120, 120 ))
  16       *     print("Thumbnail generated ok!" );
  17       *  else
  18       *     print( "There was an error generating the thumbnail" ); 
  19       * </pre>
  20       */
  21  
  22      lt_include( PLOG_CLASS_PATH."class/gallery/resizers/gallerynullresizer.class.php" );
  23      lt_include( PLOG_CLASS_PATH."class/gallery/resizers/galleryimagemagickresizer.class.php" );
  24      lt_include( PLOG_CLASS_PATH."class/gallery/resizers/gallerygdresizer.class.php" );
  25      lt_include( PLOG_CLASS_PATH."class/gallery/galleryconstants.php" );
  26  
  27      /**
  28       * \ingroup Gallery_resizer
  29       *
  30       * Takes care of generating, storing and retrieving thumbnails.
  31       *
  32       * Supports several methods for the generation of thumbnails and can support as
  33       * many as needed. It also supports caching of thumbnails so that they don't have
  34       * to be generated after each request.
  35       */
  36      class GalleryResizer  
  37      {
  38  
  39          /**
  40           * This is the array used to know which are the supported
  41           * thumbnail generator methods. To add another one, we only
  42           * have to create our own class extending the AbstractThumbnailGenerator
  43           * class and implement the methods given there.
  44           * The key of the array is the 'generatorMethod' parameter of the class
  45           * constructor and the value assigned to the key is the name of the
  46           * class that is going to generate the thumbnail.
  47           */
  48          var $_methods = Array(
  49              "default"     => "GalleryGdResizer",
  50              "gd"          => "GalleryGdResizer",
  51              "null"        => "GalleryNullResizer",
  52              "imagemagick" => "GalleryImagemagickResizer"
  53          );
  54  
  55          /**
  56           * The name of the image file
  57           * @private
  58           */
  59          var $_image;
  60  
  61          /**
  62           * The name of the method we are going to use to generate the thumbnail
  63           * @private
  64           */
  65          var $_generatorMethod;
  66  
  67          /**
  68           * Constructor. Creates a Thumbnail object from the given image file.
  69           *
  70           * @param image An Image object
  71           * @param generatorMethod Optional parameter specifying which
  72           */
  73          function GalleryResizer( $image )
  74          {
  75              
  76              
  77              // keep these things for later
  78              $this->_image = $image;
  79  
  80              // fetch some needed values from the configuration file, such as
  81              // the format we'd like to use or the backend that will finally generate
  82              // the thubmnail
  83              $config =& Config::getConfig();
  84  
  85              // the backend generator
  86              $this->_generatorMethod = $config->getValue( "thumbnail_method" );
  87              if( $this->_generatorMethod == "" )
  88                  $this->_generatorMethod = DEFAULT_GENERATOR_METHOD;
  89              // the preferred output format
  90              $this->_defaultOutputFormat = $config->getValue( "thumbnail_format" );
  91              if( $this->_defaultOutputFormat == "" )
  92                  $this->_defaultOutputFormat = THUMBNAIL_OUTPUT_FORMAT_PNG;
  93          }
  94          
  95          /**
  96           * returns the format that will be used/has been used to generate a thumbnail.
  97           *
  98           * @return Returns one of:
  99           * - THUMBNAIL_OUTPUT_FORMAT_SAME_AS_IMAGE
 100           * - THUMBNAIL_OUTPUT_FORMAT_JPG
 101           * - THUMBNAIL_OUTPUT_FORMAT_PNG
 102           * - THUMBNAIL_OUTPUT_FORMAT_GIF
 103           */
 104  		function getThumbnailFormat()
 105          {
 106              $config =& Config::getConfig();
 107              return $config->getValue( "thumbnail_format" );
 108          }
 109          
 110          /**
 111           * Generates a thumbnail.
 112           *
 113           * @param outFile
 114           * @param width The width of the thumbnail
 115           * @param height The height of the thumbnail
 116           * @param keepAspectRatio whether thumbnails should keep their aspect ratio (even though the final size
 117           *     might be somehow bigger than the value of the $height or $width parameter)
 118           * @return the path to the thumbnail that was generated or empty if error
 119           */
 120          function generate( $outFile, $width = GALLERY_DEFAULT_THUMBNAIL_WIDTH, $height = GALLERY_DEFAULT_THUMBNAIL_HEIGHT, $keepAspectRatio = true )
 121          {
 122              if( $width == "" || $width < 0 )
 123                  $width = GALLERY_DEFAULT_THUMBNAIL_WIDTH;
 124  
 125              if( $height == "" || $height < 0 )
 126                  $height = GALLERY_DEFAULT_THUMBNAIL_HEIGHT;
 127  
 128              // we can get into this 'else' if the image was not stored *or* we do not
 129              // wish to use the cached version
 130  
 131              // if it does not exist, we'll have to generate it...
 132              // find out which class is going to do to the job for us
 133              $generatorClassName = $this->_methods[$this->_generatorMethod];
 134              $generatorClassObject = new $generatorClassName( $this->_image, $this->_defaultOutputFormat );
 135              
 136              if( $this->_defaultOutputFormat != THUMBNAIL_OUTPUT_FORMAT_SAME_AS_IMAGE ) {
 137                  $fileParts = explode( ".", $outFile );
 138                  array_pop( $fileParts );
 139                  $fileNoExt = implode( ".", $fileParts );
 140                  $outFile = $fileNoExt.".".$this->_defaultOutputFormat;
 141              }
 142  
 143              $generatorClassObject->setKeepAspectRatio( $keepAspectRatio );
 144              $imgThumb = $generatorClassObject->generate( $outFile, $width, $height );
 145              
 146              return $imgThumb;
 147          }
 148      }
 149  ?>


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