Source for file ThumbBase.inc.php

Documentation is available at ThumbBase.inc.php

  1. <?php
  2. /**
  3.  * PhpThumb Base Class Definition File
  4.  * 
  5.  * This file contains the definition for the ThumbBase object
  6.  * 
  7.  * PHP Version 5 with GD 2.0+
  8.  * PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
  9.  * Copyright (c) 2009, Ian Selby/Gen X Design
  10.  * 
  11.  * Author(s): Ian Selby <ian@gen-x-design.com>
  12.  * 
  13.  * Licensed under the MIT License
  14.  * Redistributions of files must retain the above copyright notice.
  15.  * 
  16.  * @author Ian Selby <ian@gen-x-design.com>
  17.  * @copyright Copyright (c) 2009 Gen X Design
  18.  * @link http://phpthumb.gxdlabs.com
  19.  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  20.  * @version 3.0
  21.  * @package PhpThumb
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * ThumbBase Class Definition
  27.  * 
  28.  * This is the base class that all implementations must extend.  It contains the
  29.  * core variables and functionality common to all implementations, as well as the functions that
  30.  * allow plugins to augment those classes.
  31.  * 
  32.  * @package PhpThumb
  33.  * @subpackage Core
  34.  */
  35. abstract class ThumbBase 
  36. {
  37.     /**
  38.      * All imported objects
  39.      * 
  40.      * An array of imported plugin objects
  41.      * 
  42.      * @var array 
  43.      */
  44.     protected $imported;
  45.     /**
  46.      * All imported object functions
  47.      * 
  48.      * An array of all methods added to this class by imported plugin objects
  49.      * 
  50.      * @var array 
  51.      */
  52.     protected $importedFunctions;
  53.     /**
  54.      * The last error message raised
  55.      * 
  56.      * @var string 
  57.      */
  58.     protected $errorMessage;
  59.     /**
  60.      * Whether or not the current instance has any errors
  61.      * 
  62.      * @var bool 
  63.      */
  64.     protected $hasError;
  65.     /**
  66.      * The name of the file we're manipulating
  67.      * 
  68.      * This must include the path to the file (absolute paths recommended)
  69.      * 
  70.      * @var string 
  71.      */
  72.     protected $fileName;
  73.     /**
  74.      * What the file format is (mime-type)
  75.      * 
  76.      * @var string 
  77.      */
  78.     protected $format;
  79.     /**
  80.      * Whether or not the image is hosted remotely
  81.      * 
  82.      * @var bool 
  83.      */
  84.     protected $remoteImage;
  85.     
  86.     /**
  87.      * Class constructor
  88.      * 
  89.      * @return ThumbBase 
  90.      */
  91.     public function __construct ($fileName)
  92.     {
  93.         $this->imported                = array();
  94.         $this->importedFunctions    = array();
  95.         $this->errorMessage            = null;
  96.         $this->hasError                = false;
  97.         $this->fileName                = $fileName;
  98.         $this->remoteImage            = false;
  99.         
  100.         $this->fileExistsAndReadable();
  101.     }
  102.     
  103.     /**
  104.      * Imports plugins in $registry to the class
  105.      * 
  106.      * @param array $registry 
  107.      */
  108.     public function importPlugins ($registry)
  109.     {
  110.         foreach ($registry as $plugin => $meta)
  111.         {
  112.             $this->imports($plugin);
  113.         }
  114.     }
  115.     
  116.     /**
  117.      * Imports a plugin
  118.      * 
  119.      * This is where all the plugins magic happens!  This function "loads" the plugin functions, making them available as
  120.      * methods on the class.
  121.      * 
  122.      * @param string $object The name of the object to import / "load"
  123.      */
  124.     protected function imports ($object)
  125.     {
  126.         // the new object to import
  127.         $newImport             new $object();
  128.         // the name of the new object (class name)
  129.         $importName            get_class($newImport);
  130.         // the new functions to import
  131.         $importFunctions     get_class_methods($newImport);
  132.         
  133.         // add the object to the registry
  134.         array_push($this->importedarray($importName$newImport));
  135.         
  136.         // add the methods to the registry
  137.         foreach ($importFunctions as $key => $functionName)
  138.         {
  139.             $this->importedFunctions[$functionName&$newImport;
  140.         }
  141.     }
  142.     
  143.     /**
  144.      * Checks to see if $this->fileName exists and is readable
  145.      * 
  146.      */
  147.     protected function fileExistsAndReadable ()
  148.     {
  149.         if (stristr($this->fileName'http://'!== false)
  150.         {
  151.             $this->remoteImage = true;
  152.             return;
  153.         }
  154.         
  155.         if (!file_exists($this->fileName))
  156.         {
  157.             $this->triggerError('Image file not found: ' $this->fileName);
  158.         }
  159.         elseif (!is_readable($this->fileName))
  160.         {
  161.             $this->triggerError('Image file not readable: ' $this->fileName);
  162.         }
  163.     }
  164.     
  165.     /**
  166.      * Sets $this->errorMessage to $errorMessage and throws an exception
  167.      * 
  168.      * Also sets $this->hasError to true, so even if the exceptions are caught, we don't
  169.      * attempt to proceed with any other functions
  170.      * 
  171.      * @param string $errorMessage 
  172.      */
  173.     protected function triggerError ($errorMessage)
  174.     {
  175.         $this->hasError     = true;
  176.         $this->errorMessage    = $errorMessage;
  177.         
  178.         throw new Exception ($errorMessage);
  179.     }
  180.     
  181.     /**
  182.      * Calls plugin / imported functions
  183.      * 
  184.      * This is also where a fair amount of plugins magaic happens.  This magic method is called whenever an "undefined" class
  185.      * method is called in code, and we use that to call an imported function.
  186.      * 
  187.      * You should NEVER EVER EVER invoke this function manually.  The universe will implode if you do... seriously ;)
  188.      * 
  189.      * @param string $method 
  190.      * @param array $args 
  191.      */
  192.     public function __call ($method$args)
  193.     {
  194.         ifarray_key_exists($method$this->importedFunctions))
  195.         {
  196.             $args[$this;
  197.             return call_user_func_array(array($this->importedFunctions[$method]$method)$args);
  198.         }
  199.         
  200.         throw new BadMethodCallException ('Call to undefined method/class function: ' $method);
  201.     }
  202.  
  203.     /**
  204.      * Returns $imported.
  205.      * @see ThumbBase::$imported
  206.      * @return array 
  207.      */
  208.     public function getImported ()
  209.     {
  210.         return $this->imported;
  211.     }
  212.     
  213.     /**
  214.      * Returns $importedFunctions.
  215.      * @see ThumbBase::$importedFunctions
  216.      * @return array 
  217.      */
  218.     public function getImportedFunctions ()
  219.     {
  220.         return $this->importedFunctions;
  221.     }
  222.     
  223.     /**
  224.      * Returns $errorMessage.
  225.      *
  226.      * @see ThumbBase::$errorMessage
  227.      */
  228.     public function getErrorMessage ()
  229.     {
  230.         return $this->errorMessage;
  231.     }
  232.     
  233.     /**
  234.      * Sets $errorMessage.
  235.      *
  236.      * @param object $errorMessage 
  237.      * @see ThumbBase::$errorMessage
  238.      */
  239.     public function setErrorMessage ($errorMessage)
  240.     {
  241.         $this->errorMessage = $errorMessage;
  242.     }
  243.     
  244.     /**
  245.      * Returns $fileName.
  246.      *
  247.      * @see ThumbBase::$fileName
  248.      */
  249.     public function getFileName ()
  250.     {
  251.         return $this->fileName;
  252.     }
  253.     
  254.     /**
  255.      * Sets $fileName.
  256.      *
  257.      * @param object $fileName 
  258.      * @see ThumbBase::$fileName
  259.      */
  260.     public function setFileName ($fileName)
  261.     {
  262.         $this->fileName = $fileName;
  263.     }
  264.     
  265.     /**
  266.      * Returns $format.
  267.      *
  268.      * @see ThumbBase::$format
  269.      */
  270.     public function getFormat ()
  271.     {
  272.         return $this->format;
  273.     }
  274.     
  275.     /**
  276.      * Sets $format.
  277.      *
  278.      * @param object $format 
  279.      * @see ThumbBase::$format
  280.      */
  281.     public function setFormat ($format)
  282.     {
  283.         $this->format = $format;
  284.     }
  285.     
  286.     /**
  287.      * Returns $hasError.
  288.      *
  289.      * @see ThumbBase::$hasError
  290.      */
  291.     public function getHasError ()
  292.     {
  293.         return $this->hasError;
  294.     }
  295.     
  296.     /**
  297.      * Sets $hasError.
  298.      *
  299.      * @param object $hasError 
  300.      * @see ThumbBase::$hasError
  301.      */
  302.     public function setHasError ($hasError)
  303.     {
  304.         $this->hasError = $hasError;
  305.     
  306.     
  307.  
  308. }

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