Source for file thumbnail.class.php

Documentation is available at thumbnail.class.php

  1. <?php
  2. /**
  3.  * PhpThumb Library Definition File
  4.  *
  5.  * This file contains the definitions for the PhpThumbFactory class.
  6.  * It also includes the other required base class files.
  7.  *
  8.  * If you've got some auto-loading magic going on elsewhere in your code, feel free to
  9.  * remove the include_once statements at the beginning of this file... just make sure that
  10.  * these files get included one way or another in your code.
  11.  *
  12.  * PHP Version 5 with GD 2.0+
  13.  * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
  14.  * Copyright (c) 2009, Ian Selby/Gen X Design
  15.  *
  16.  * Author(s): Ian Selby <ian@gen-x-design.com>
  17.  *
  18.  * Licensed under the MIT License
  19.  * Redistributions of files must retain the above copyright notice.
  20.  *
  21.  * @author Ian Selby <ian@gen-x-design.com>
  22.  * @copyright Copyright (c) 2009 Gen X Design
  23.  * @link http://phpthumb.gxdlabs.com
  24.  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  25.  * @version 3.0
  26.  * @package PhpThumb
  27.  * @filesource
  28.  */
  29.  
  30. // define some useful constants
  31. define('THUMBLIB_BASE_PATH'dirname(__FILE__));
  32. define('THUMBLIB_PLUGIN_PATH'THUMBLIB_BASE_PATH '/thumb_plugins/');
  33.  
  34. /**
  35.  * Include the PhpThumb Class
  36.  */
  37. require_once THUMBLIB_BASE_PATH '/PhpThumb.inc.php';
  38. /**
  39.  * Include the ThumbBase Class
  40.  */
  41. require_once THUMBLIB_BASE_PATH '/ThumbBase.inc.php';
  42. /**
  43.  * Include the GdThumb Class
  44.  */
  45. require_once THUMBLIB_BASE_PATH '/GdThumb.inc.php';
  46.  
  47. /**
  48.  * PhpThumbFactory Object
  49.  *
  50.  * This class is responsible for making sure everything is set up and initialized properly,
  51.  * and returning the appropriate thumbnail class instance.  It is the only recommended way
  52.  * of using this library, and if you try and circumvent it, the sky will fall on your head :)
  53.  *
  54.  * Basic use is easy enough.  First, make sure all the settings meet your needs and environment...
  55.  * these are the static variables defined at the beginning of the class.
  56.  *
  57.  * Once that's all set, usage is pretty easy.  You can simply do something like:
  58.  * <code>$thumb = PhpThumbFactory::create('/path/to/file.png');</code>
  59.  *
  60.  * Refer to the documentation for the create function for more information
  61.  *
  62.  * @package PhpThumb
  63.  * @subpackage Core
  64.  */
  65. class thumbnail
  66. {
  67.     /**
  68.      * Which implemenation of the class should be used by default
  69.      *
  70.      * Currently, valid options are:
  71.      *  - imagick
  72.      *  - gd
  73.      *
  74.      * These are defined in the implementation map variable, inside the create function
  75.      *
  76.      * @var string 
  77.      */
  78.     public static $defaultImplemenation DEFAULT_THUMBLIB_IMPLEMENTATION;
  79.     /**
  80.      * Where the plugins can be loaded from
  81.      *
  82.      * Note, it's important that this path is properly defined.  It is very likely that you'll
  83.      * have to change this, as the assumption here is based on a relative path.
  84.      *
  85.      * @var string 
  86.      */
  87.     public static $pluginPath THUMBLIB_PLUGIN_PATH;
  88.  
  89.     /**
  90.      * Factory Function
  91.      *
  92.      * This function returns the correct thumbnail object, augmented with any appropriate plugins.
  93.      * It does so by doing the following:
  94.      *  - Getting an instance of PhpThumb
  95.      *  - Loading plugins
  96.      *  - Validating the default implemenation
  97.      *  - Returning the desired default implementation if possible
  98.      *  - Returning the GD implemenation if the default isn't available
  99.      *  - Throwing an exception if no required libraries are present
  100.      *
  101.      * @return GdThumb 
  102.      * @uses PhpThumb
  103.      * @param string $filename The path and file to load [optional]
  104.      */
  105.     public static function create ($filename null$options array())
  106.     {
  107.         // map our implementation to their class names
  108.         $implementationMap array
  109.         (
  110.             'imagick'    => 'ImagickThumb',
  111.             'gd'         => 'GdThumb'
  112.         );
  113.  
  114.         // grab an instance of PhpThumb
  115.         $pt PhpThumb::getInstance();
  116.         // load the plugins
  117.         $pt->loadPlugins(self::$pluginPath);
  118.  
  119.         $toReturn null;
  120.         $implementation self::$defaultImplemenation;
  121.  
  122.         // attempt to load the default implementation
  123.         if ($pt->isValidImplementation(self::$defaultImplemenation))
  124.         {
  125.             $imp $implementationMap[self::$defaultImplemenation];
  126.             $toReturn new $imp($filename$options);
  127.         }
  128.         // load the gd implementation if default failed
  129.         else if ($pt->isValidImplementation('gd'))
  130.         {
  131.             $imp $implementationMap['gd'];
  132.             $implementation 'gd';
  133.             $toReturn new $imp($filename$options);
  134.         }
  135.         // throw an exception if we can't load
  136.         else
  137.         {
  138.             throw new Exception('You must have either the GD or iMagick extension loaded to use this library');
  139.         }
  140.  
  141.         $registry $pt->getPluginRegistry($implementation);
  142.         $toReturn->importPlugins($registry);
  143.         return $toReturn;
  144.     }
  145. }

Documentation generated on Tue, 09 Aug 2011 09:05:39 +0200 by phpDocumentor 1.4.3