Image_Tools
[ class tree: Image_Tools ] [ index: Image_Tools ] [ all elements ]

Source for file Tools.php

Documentation is available at Tools.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. // {{{ Header
  5.  
  6. /**
  7.  * This is a main file of Image_Tools package.
  8.  *
  9.  * PHP versions 4 and 5
  10.  *
  11.  * LICENSE:
  12.  *
  13.  * Copyright (c) 2005-2006 Tobias Schlitt <toby@php.net>,
  14.  * Firman Wandayandi <firman@php.net>
  15.  * All rights reserved.
  16.  *
  17.  * Redistribution and use in source and binary forms, with or without
  18.  * modification, are permitted under the terms of the BSD License.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  *
  33.  * @category    Images
  34.  * @package     Image_Tools
  35.  * @author      Tobias Schlitt <toby@php.net>
  36.  * @author      Firman Wandayandi <firman@php.net>
  37.  * @copyright   Copyright (c) 2003-2006 Tobias Schlitt <toby@php.net>,
  38.  *               Firman Wandayandi <firman@php.net>
  39.  * @license     http://www.opensource.org/licenses/bsd-license.php
  40.  *               BSD License
  41.  * @version     CVS: $Id: Tools.php,v 1.7 2006/11/23 22:17:31 firman Exp $
  42.  */
  43.  
  44. // }}}
  45. // {{{ Dependencies
  46.  
  47.  
  48.  
  49. /**
  50.  * load PEAR for error handling.
  51.  */
  52. require_once 'PEAR.php';
  53.  
  54. /**
  55.  * load PHP_Compat for PHP backward compatibility.
  56.  */
  57. require_once 'PHP/Compat.php';
  58.  
  59. /**
  60.  * is_a(), since PHP 4.2.0
  61.  */
  62. @PHP_Compat::loadFunction('is_a');
  63.  
  64. /**
  65.  * image_type_to_mime_type, since PHP 4.3.0
  66.  */
  67. @PHP_Compat::loadFunction('image_type_to_mime_type');
  68.  
  69. // }}}
  70. // {{{ Constants
  71.  
  72.  
  73.  
  74. /**
  75.  * Image_Tools error, indicating that the given tool was not found.
  76.  *
  77.  * @name    IMAGE_TOOLS_BASE_ERR_CLASS_INVALID
  78.  * @access  public
  79.  */
  80. define('IMAGE_TOOLS_BASE_ERR_CLASS_INVALID'-1);
  81.  
  82. /**
  83.  * Image_Tools error, indicating that the given tool object could not be
  84.  * instanciated.
  85.  *
  86.  * @name    IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED
  87.  * @access  public
  88.  */
  89. define('IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED'-2);
  90.  
  91. /**
  92.  * Image_Tools error, indicating that you may not instanciate
  93.  * the base class Image_Tools directly.
  94.  *
  95.  * @name    IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED
  96.  * @access  public
  97.  */
  98. define('IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FORBIDEN'-3);
  99.  
  100. /**
  101.  * Image_Tools error, indicating that the given option was of a wrong type.
  102.  *
  103.  * @name    IMAGE_TOOLS_BASE_ERR_OPTION_INVALID
  104.  * @access  public
  105.  */
  106. define('IMAGE_TOOLS_BASE_ERR_OPTION_INVALID'-4);
  107.  
  108. /**
  109.  * Image_Tools error, indicating that the given option is not
  110.  * supported by the tool.
  111.  *
  112.  * @name    IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
  113.  * @access  public
  114.  */
  115. define('IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED'-5);
  116.  
  117. /**
  118.  * Image_Tools error, indicating that the given method is not
  119.  * supported by the tool.
  120.  *
  121.  * @name    IMAGE_TOOLS_BASE_ERR_METHOD_UNSUPPORTED
  122.  * @access  public
  123.  */
  124. define('IMAGE_TOOLS_BASE_ERR_METHOD_UNSUPPORTED'-6);
  125.  
  126. /**
  127.  * Image_Tools error, indicating that the given option is not
  128.  * set.
  129.  *
  130.  * @name    IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET
  131.  * @access  public
  132.  */
  133. define('IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET'-7);
  134.  
  135. /**
  136.  * Image_Tools error, indicating that the HTTP headers have been sent
  137.  * before the display() method was called. Ensure that no output started
  138.  * before that.
  139.  *
  140.  * @name    IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED
  141.  * @access  public
  142.  */
  143. define('IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED'-8);
  144.  
  145. /**
  146.  * Image_Tools error, indicating that the image type you selected for
  147.  * displaying the image is not supported. Please use a supported image
  148.  * type.
  149.  *
  150.  * @name    IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
  151.  * @access  public
  152.  */
  153. define('IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED'-9);
  154.  
  155. /**
  156.  * Image_Tools error, indicating that the image could not be saved.
  157.  * Check the permissions on the desired path first.
  158.  *
  159.  * @name    IMAGE_TOOLS_BASE_ERR_SAVEIMAGE_FAILED
  160.  * @access  public
  161.  */
  162. define('IMAGE_TOOLS_BASE_ERR_SAVEIMAGE_FAILED'-10);
  163.  
  164. /**
  165.  * Image_Tools error, indicating that you called a static method non
  166.  * statically, which may only be called statically.
  167.  *
  168.  * @name    IMAGE_TOOLS_BASE_ERR_NONSTATIC_FAILED
  169.  * @access  public
  170.  */
  171. define('IMAGE_TOOLS_BASE_ERR_NONSTATIC_FAILED'-11);
  172.  
  173. /**
  174.  * Image_Tools error, indicating that you called a static method
  175.  * statically, which may only be called non statically.
  176.  *
  177.  * @name    IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  178.  * @access  public
  179.  */
  180. define('IMAGE_TOOLS_BASE_ERR_STATIC_FAILED'-12);
  181.  
  182. // }}}
  183. // {{{ Class: Image_Tools
  184.  
  185.  
  186.  
  187. /**
  188.  * This is the Image_Tools base class
  189.  * Every image-tool has to derive from this class and implement several methods
  190.  * itself. Other methods are implemented in the base class directly, but may
  191.  * only be called on the sub-classes. The only method being called directly from
  192.  * the base class is the factory() method.
  193.  *
  194.  * @category    Images
  195.  * @package     Image_Tools
  196.  * @author      Tobias Schlitt <toby@php.net>
  197.  * @author      Firman Wandayandi <firman@php.net>
  198.  * @copyright   Copyright (c) 2003-2006 Tobias Schlitt <toby@php.net>,
  199.  *               Firman Wandayandi <firman@php.net>
  200.  * @license     http://www.opensource.org/licenses/bsd-license.php
  201.  *               BSD License
  202.  * @version     Release: 0.4.1
  203.  */
  204. {
  205.     // {{{ Properties
  206.  
  207.     
  208.  
  209.     /**
  210.      * Contain the options inside all subclasses.
  211.      *
  212.      * @var array 
  213.      * @access protected
  214.      */
  215.     var $options = array();
  216.  
  217.     /**
  218.      * Has to contain all available options of a subclass. The index of
  219.      * the array is the option name, the value is either a PHP variable
  220.      * type (e.g. int, string, float, resource,...) or a class name
  221.      * (e.g. Image_Tools, DB,...). Also allowed the are the special values
  222.      * 'number' and 'mixed'. The structure of that array is as follows:
  223.      *
  224.      * $availableOptions = array(
  225.      *      'foo'   =>  'int',
  226.      *      'bar'   =>  'float',
  227.      *      'db'    =>  'DB',
  228.      *      'img'   =>  'resource'
  229.      * );
  230.      *
  231.      * @var array 
  232.      * @access protected
  233.      */
  234.     var $availableOptions = array();
  235.  
  236.     /**
  237.      * Has to contain all available methods (as the keys of the array)
  238.      * and the expected parameters (in an array) as the values. The structure
  239.      * of this array is as follows:
  240.      *
  241.      * $availableMethods = array(
  242.      *      'myMethod'  =>  array(
  243.      *          'foo'   =>  'int',
  244.      *          'db'    =>  'DB'
  245.      *      'fooBar'    =>  array(
  246.      *          'baz'   =>  'resource',
  247.      *          'bun'   =>  'mixed'
  248.      *      )
  249.      * );
  250.      *
  251.      * @var array 
  252.      * @access protected
  253.      */
  254.     var $availableMethods = array();
  255.  
  256.     /**
  257.      * Contains the version of the specific subclass. A version has to follow the PEAR
  258.      * version naming standard, which can be found here:
  259.      * http://pear.php.net/group/docs/20040226-vn.php
  260.      *
  261.      * @var string 
  262.      * @access protected
  263.      */
  264.     var $version = '';
  265.  
  266.     /**
  267.      * Contains the api-version of the baseclass. The API version has to follow the
  268.      * PEAR version naming standard.
  269.      *
  270.      * @var string 
  271.      * @access protected
  272.      */
  273.     var $apiVersion = '0.1';
  274.  
  275.     /**
  276.      * Result rendered image.
  277.      *
  278.      * @var resource 
  279.      * @access protected
  280.      */
  281.     var $resultImage = null;
  282.  
  283.     // }}}
  284.     // {{{ Constructor
  285.  
  286.     
  287.  
  288.     /**
  289.      * Constructor
  290.      *
  291.      * this method has no purpose for Image_Tools, but defines the API for
  292.      * all base classes. Calling this directly with $foo = new Image_Tools(...);
  293.      * will cause an error.
  294.      *
  295.      * @param   array $options optional Options.
  296.      * @access  protected
  297.      * @see     IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FORBIDEN
  298.      */
  299.     function Image_Tools($options = array())
  300.     {
  301.         PEAR::setErrorHandling(PEAR_ERROR_TRIGGER);
  302.         if (is_a($this'Image_Tools'&& !is_subclass_of($this'Image_Tools')) {
  303.             PEAR::raiseError('Cannot instanciate Image_Tools directly',
  304.                              IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FORBIDEN);
  305.         }
  306.  
  307.         $this->set($options);
  308.         PEAR::setErrorHandling(PEAR_ERROR_RETURN);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ factory()
  313.  
  314.     
  315.  
  316.     /**
  317.      * Create a new instance of an image tool
  318.      *
  319.      * This method will create a new image tool, defined by the $tool var,
  320.      * which is a string it will call the constructor of the specific tool
  321.      * and return it (if no error occurs)
  322.      *
  323.      * @param string $tool Tool name
  324.      * @param array $options optional Options
  325.      *
  326.      * @return  object New image tool on success or PEAR_Error on failure
  327.      * @access  public
  328.      * @static
  329.      * @see     IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED,
  330.      *           IMAGE_TOOLS_BASE_ERR_CLASS_INVALID,
  331.      *           IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
  332.      *           IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
  333.      */
  334.     function &factory($tool$options = array())
  335.     {
  336.         if (isset($this&& strtolower(get_class($this)) == 'image_tools'{
  337.             return PEAR::raiseError('This method may only be called statically',
  338.                                     IMAGE_TOOLS_BASE_ERR_NONSTATIC_FAILED);
  339.         }
  340.  
  341.         // Renice the tool name
  342.         $toolParts @explode('_'$tool);
  343.         if (empty($toolParts|| !is_array($toolParts)) {
  344.             $toolParts[$tool;
  345.         }
  346.  
  347.         foreach ($toolParts as $key => $value{
  348.             $toolParts[$keyucfirst($value);
  349.         }
  350.         $tool implode('_'$toolParts);
  351.         $toolPath 'Image/Tools/'.$tool.'.php';
  352.  
  353.         // Include the tool class
  354.         require_once($toolPath);
  355.  
  356.         $className = "Image_Tools_${tool}";
  357.         if (!class_exists($className)) {
  358.             return PEAR::raiseError('File not found ' $toolPath .
  359.                                     ' or undefined class '.$className,
  360.                                     IMAGE_TOOLS_BASE_ERR_CLASS_INVALID);
  361.         }
  362.  
  363.         @$obj =new $className($options);
  364.         if (!is_object($obj|| !is_a($obj$className)) {
  365.             return PEAR::raiseError('Could not instanciate image tool '.$className,
  366.                                     IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED);
  367.         }
  368.         $res $obj->set($options);
  369.         if (PEAR::isError($res)) {
  370.             return $res;
  371.         }
  372.         return $obj;
  373.     }
  374.  
  375.     // }}}
  376.     // {{{ set()
  377.  
  378.     
  379.  
  380.     /**
  381.      * Set the option(s)
  382.      * Set a single or multiple options. The parameter of that method may
  383.      * either be a single options array or a key/value pair setting a single
  384.      * option. This method has not to be reimplemented by the Image_Tools
  385.      * sub classes.
  386.      *
  387.      * @param mixed $option A single option name or the options array
  388.      * @param mixed $value Option value if $option is string
  389.      *
  390.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure
  391.      * @access  protected
  392.      * @see     IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
  393.      *           IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
  394.      */
  395.     function set($option$value = null)
  396.     {
  397.         if (!isset($this)) {
  398.             return PEAR::raiseError('This method may only be called non statically',
  399.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  400.         }
  401.         if (is_array($option)) {
  402.             foreach ($option as $key => $value{
  403.                 $res $this->set($key@$value);
  404.                 if (PEAR::isError($res)) {
  405.                     return $res;
  406.                 }
  407.             }
  408.             return true;
  409.         }
  410.         $res $this->isValidOption($option@$value);
  411.         if (PEAR::isError($res)) {
  412.             return $res;
  413.         }
  414.         $this->options[$option$value;
  415.         return true;
  416.     }
  417.  
  418.     // }}}
  419.     // {{{ isValidOption()
  420.  
  421.     
  422.  
  423.     /**
  424.      * Has the option a valid value?
  425.      * Determines, if the value given is valid for the option.
  426.      *
  427.      * @param mixed $option A single option name
  428.      * @param mixed $value Option value
  429.      *
  430.      * @return  bool|PEAR_ErrorTRUE on acceptans or PEAR_Error on failure
  431.      * @access  protected
  432.      * @see     IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
  433.      *           IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
  434.      */
  435.     function isValidOption($name$value)
  436.     {
  437.         $res $this->supportsOption($name);
  438.         if (PEAR::isError($res)) {
  439.             return $res;
  440.         }
  441.         $type $this->availableOptions[$name];
  442.         switch ($type{
  443.             case 'mixed':
  444.                 return true;
  445.                 break;
  446.             case 'bool':
  447.             case 'int':
  448.             case 'float':
  449.             case 'double':
  450.             case 'string':
  451.             case 'array':
  452.             case 'resource':
  453.             case 'object':
  454.                 $typeCheck 'is_'.$type;
  455.                 if ($typeCheck($value)) {
  456.                     return true;
  457.                 }
  458.                 break;
  459.             case 'number':
  460.                 if (is_int($value|| is_float($value)) {
  461.                     return true;
  462.                 }
  463.                 break;
  464.             default:
  465.                 if (is_a($value$type)) {
  466.                     return true;
  467.                 }
  468.                 break;
  469.         }
  470.         return PEAR::raiseError('Wrong type for option ' $name .
  471.                                 '. Requires '$type .', but is ' gettype($value),
  472.                                 IMAGE_TOOLS_BASE_ERR_OPTION_INVALID);
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ get()
  477.  
  478.     
  479.  
  480.     /**
  481.      * Get the value of the option
  482.      *
  483.      * returns the value of an option of, an error if the option is not
  484.      * available.
  485.      *
  486.      * @param string $option Option name.
  487.      *
  488.      * @return  mixed|PEAR_ErrorOption value on success or
  489.      *                            PEAR_Error on failure.
  490.      * @access  public
  491.      * @see     IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED,
  492.      *           IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET,
  493.      *           IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  494.      */
  495.     function get($option)
  496.     {
  497.         if (!isset($this)) {
  498.             return PEAR::raiseError('This method may only be called non statically',
  499.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  500.         }
  501.         $res $this->supportsOption($option);
  502.         if (PEAR::isError($res)) {
  503.             return $res;
  504.         }
  505.         if (isset($this->options[$option])) {
  506.             return $this->options[$option];
  507.         }
  508.         return PEAR::raiseError('Option '.$option.' not set',
  509.                                 IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET);
  510.     }
  511.  
  512.     // }}}
  513.     // {{{ createImageFromFile()
  514.  
  515.     
  516.  
  517.     /**
  518.      * Create a GD image resource from file (JPEG, PNG, WBMP and XBM support).
  519.      *
  520.      * @param string $filename The image filename.
  521.      *
  522.      * @return mixed GD image resource on success, PEAR_Error on failure.
  523.      * @access public
  524.      * @static
  525.      */
  526.     function createImageFromFile($filename)
  527.     {
  528.         if (!is_file($filename|| !is_readable($filename)) {
  529.             return PEAR::raiseError('Unable to open file "' $filename '"');
  530.         }
  531.  
  532.         // determine image format
  533.         list, , $imgTypegetimagesize($filename);
  534.  
  535.         switch ($imgType{
  536.             case IMAGETYPE_JPEG:
  537.                 return imagecreatefromjpeg($filename);
  538.                 break;
  539.             case IMAGETYPE_PNG:
  540.                 return imagecreatefrompng($filename);
  541.                 break;
  542.             case IMAGETYPE_WBMP:
  543.                 return imagecreatefromwbmp($filename);
  544.                 break;
  545.             case IMAGETYPE_XBM:
  546.                 return imagecreatefromxbm($filename);
  547.                 break;
  548.             default:
  549.                 return PEAR::raiseError('Unsupport image type');
  550.         }
  551.     }
  552.  
  553.     // }}}
  554.     // {{{ createImageFromString()
  555.  
  556.     
  557.  
  558.     /**
  559.      * Create a GD image resource from a string data.
  560.      *
  561.      * @param string $data The string image data.
  562.      *
  563.      * @return mixed GD image resource on success, PEAR_Error on failure.
  564.      * @access public
  565.      * @static
  566.      */
  567.     function createImageFromString($data)
  568.     {
  569.         if (!is_string($data|| empty($data)) {
  570.             PEAR::raiseError('Invalid data value.');
  571.         }
  572.  
  573.         $img imagecreatefromstring($data);
  574.         if ($img === false{
  575.             return PEAR::raiseError('Failed to create image from string data');
  576.         }
  577.         return $img;
  578.     }
  579.  
  580.     // }}}
  581.     // {{{ createImage()
  582.  
  583.     
  584.  
  585.     /**
  586.      * Create a GD image resource from given input.
  587.      *
  588.      * This method tried to detect what the input, if it is a file the
  589.      * createImageFromFile will be called, otherwise createImageFromString().
  590.      *
  591.      * @param   mixed $input The input for creating an image resource. The value
  592.      *                        may a string of filename, string of image data or
  593.      *                        GD image resource.
  594.      *
  595.      * @return  resource|PEAR_ErrorAn GD image resource on success or
  596.      *                               PEAR_Error on failure.
  597.      * @access  public
  598.      * @see     createImageFromFile()
  599.      * @see     createImageFromString()
  600.      */
  601.     function createImage($input)
  602.     {
  603.         if (is_file($input)) {
  604.             return Image_Tools::createImageFromFile($input);
  605.         else if (is_string($input)) {
  606.             return Image_Tools::createImageFromString($input);
  607.         else if (Image_Tools::isGDImageResource($input)) {
  608.             return $input;
  609.         }
  610.         return PEAR::raiseError('Invalid input given.');
  611.     }
  612.  
  613.     // }}}
  614.     // {{{ isGDImageResource()
  615.  
  616.     
  617.  
  618.     /**
  619.      * Find the whether a value is the GD image resource or not.
  620.      *
  621.      * @param mixed $value Value to evaluate.
  622.      *
  623.      * @return bool TRUE if a value is the GD image resource, otherwise FALSE.
  624.      * @access public
  625.      * @static
  626.      */
  627.     function isGDImageResource($value)
  628.     {
  629.         if (is_resource($value&& get_resource_type($value== 'gd'{
  630.             return true;
  631.         }
  632.         return false;
  633.     }
  634.  
  635.     // }}}
  636.     // {{{ render()
  637.  
  638.     
  639.  
  640.     /**
  641.      * Render the result of a tool to the given image
  642.      *
  643.      * Since that's the purpose of all image tools, this method should stay
  644.      * with it's api as is. The rendering itself must be tool specific. The
  645.      * method gets a GD2 image as it's parameter. The creation of images inside
  646.      * Image_Tools is not supported.
  647.      *
  648.      * @access  protected
  649.      * @see     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  650.      */
  651.     function render()
  652.     {
  653.         if (!isset($this)) {
  654.             return PEAR::raiseError('This method may only be called non statically',
  655.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  656.         }
  657.     }
  658.  
  659.     // }}}
  660.     // {{{ getResultImage()
  661.  
  662.     
  663.  
  664.     /**
  665.      * Get rendered image.
  666.      *
  667.      * @return resource The GD image resource of rendered result.
  668.      * @access public
  669.      */
  670.     function getResultImage()
  671.     {
  672.         return $this->resultImage;
  673.     }
  674.  
  675.     // }}}
  676.     // {{{ display()
  677.  
  678.     
  679.  
  680.     /**
  681.      * Display rendered image (send it to browser).
  682.      * This method is a common implementation to render and output an image.
  683.      * The method calls the render() method automatically and outputs the
  684.      * image to the browser.
  685.      *
  686.      * @param   int $type optional Type of format image (use defined image
  687.      *                              type constants), default is IMAGETYPE_PNG.
  688.      *
  689.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  690.      * @access  public
  691.      * @see     IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED,
  692.      *           IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
  693.      */
  694.     function display($type = IMAGETYPE_PNG)
  695.     {
  696.         $res $this->render();
  697.         if (PEAR::isError($res)) {
  698.             return PEAR::raiseError('Error rendering: ' $res->getMessage());
  699.         }
  700.  
  701.         $ctype image_type_to_mime_type($type);
  702.         if (!headers_sent()) {
  703.             header('Content-Type: ' $ctype);
  704.         else {
  705.             return PEAR::raiseError('Headers have already been sent. Could not display image.',
  706.                                     IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED);
  707.         }
  708.  
  709.         switch ($type{
  710.             case IMAGETYPE_PNG:
  711.                 imagepng($this->resultImage);
  712.                 break;
  713.             case IMAGETYPE_JPEG:
  714.                 imagejpeg($this->resultImage);
  715.                 break;
  716.             case IMAGETYPE_WBMP:
  717.                 imagewbmp($this->resultImage);
  718.                 break;
  719.             default:
  720.                 return PEAR::raiseError('Image type '.$ctype.' not supported by PHP',
  721.                                         IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED);
  722.                 break;
  723.         }
  724.         return true;
  725.     }
  726.  
  727.     // }}}
  728.     // {{{ save()
  729.  
  730.     
  731.  
  732.     /**
  733.      * Save rendered image to a file.
  734.      *
  735.      * @param string $path Path to destination filename.
  736.      * @param int $type optional Type of image format (use defined image type
  737.      *                            constants), default is IMAGETYPE_PNG.
  738.      *
  739.      * @return  bool|PEAR_ErrorTRUE on success, PEAR_Error on failure.
  740.      * @access  public
  741.      * @see     IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED,
  742.                 IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
  743.      */
  744.     function save($path$type = IMAGETYPE_PNG)
  745.     {
  746.         $res $this->render();
  747.         if (PEAR::isError($res)) {
  748.             return PEAR::raiseError('Error rendering: ' $resultImage->getMessage());
  749.         }
  750.  
  751.         switch ($type{
  752.             case IMAGETYPE_PNG:
  753.                 $res imagepng($this->resultImage$path);
  754.                 break;
  755.             case IMAGETYPE_JPEG:
  756.                 $res imagejpeg($this->resultImage$path);
  757.                 break;
  758.             case IMAGETYPE_WBMP:
  759.                 $res imagewbmp($this->resultImage$path);
  760.                 break;
  761.             default:
  762.                 return PEAR::raiseError('Image type '.$ctype.' not supported by PHP',
  763.                                         IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED);
  764.                 break;
  765.         }
  766.  
  767.         if (!$res{
  768.             PEAR::raiseError('Saving image '.$path.' failed',
  769.                              IMAGE_TOOLS_BASE_ERR_SAVEIMAGE_FAILED);
  770.         }
  771.         return true;
  772.     }
  773.  
  774.     // }}}
  775.     // {{{ supportsOption()
  776.  
  777.     
  778.  
  779.     /**
  780.      * Find out the whether a subclass supports the option
  781.      *
  782.      * @param string $option Option name.
  783.      *
  784.      * @return  bool TRUE if supported, otherwise FALSE
  785.      * @access  protected
  786.      * @see     IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED,
  787.      *           IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  788.      */
  789.     function supportsOption($option)
  790.     {
  791.         if (!isset($this)) {
  792.             return PEAR::raiseError('This method may only be called non statically',
  793.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  794.         }
  795.         if (isset($this->availableOptions[$option])) {
  796.             return true;
  797.         }
  798.         return PEAR::raiseError('Unsupported option '.$option,
  799.                                 IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED);
  800.     }
  801.  
  802.     // }}}
  803.     // {{{ availableOptions()
  804.  
  805.     
  806.  
  807.     /**
  808.      * Get all available options of the subclass
  809.      *
  810.      * @return  array Available options.
  811.      * @access  protected
  812.      * @see     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  813.      */
  814.     function availableOptions()
  815.     {
  816.         if (!isset($this)) {
  817.             return PEAR::raiseError('This method may only be called non statically',
  818.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  819.         }
  820.         return $this->availableOptions;
  821.     }
  822.  
  823.     // }}}
  824.     // {{{ supportsMethod()
  825.  
  826.     
  827.  
  828.     /**
  829.      * Get the method parameters
  830.      *
  831.      * @param   string $methodName Method name.
  832.      *
  833.      * @return  array|boolarray of the parameters a method expects or
  834.      *                      FALSE if the method is not available.
  835.      * @access  protected
  836.      * @see     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  837.      */
  838.     function supportsMethod($methodName)
  839.     {
  840.         if (!isset($this)) {
  841.             return PEAR::raiseError('This method may only be called non statically',
  842.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  843.         }
  844.         if (isset($this->availableMethods[$methodName])) {
  845.             return $this->availableMethods[$methodName];
  846.         }
  847.         return PEAR::raiseError('Unsupported method '.$methodName,
  848.                                 IMAGE_TOOLS_BASE_ERR_METHOD_UNSUPPORTED);
  849.     }
  850.  
  851.     // }}}
  852.     // {{{ availableMethods()
  853.  
  854.     
  855.  
  856.     /**
  857.      * Get available methods
  858.      *
  859.      * returns an array where the keys are the method names available
  860.      * and the value for each key is an array containing the
  861.      * parameter the specific method expects.
  862.      *
  863.      * @return  array Available methods.
  864.      * @access  protected
  865.      * @see     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
  866.      */
  867.     function availableMethods()
  868.     {
  869.         if (!isset($this)) {
  870.             return PEAR::raiseError('This method may only be called non statically',
  871.                                     IMAGE_TOOLS_BASE_ERR_STATIC_FAILED);
  872.         }
  873.         return $this->availableMethods;
  874.     }
  875.  
  876.     // }}}
  877.     // {{{ getAPIVersion()
  878.  
  879.     
  880.  
  881.     /**
  882.      * Get the API version of the common base
  883.      *
  884.      * This methods can be called statically using Image_Tools::getAPIVersion() or
  885.      * from the subclass e.g Image_Tools_Border::getAPIVersion() or
  886.      * $border->getAPIVersion()
  887.      *
  888.      * @return string Image_Tools base class api-version
  889.      * @access protected
  890.      */
  891.     function getAPIVersion()
  892.     {
  893.         if (isset($this)) {
  894.             return $this->apiVersion;
  895.         else {
  896.             $obj = new Image_Tools;
  897.             return $obj->getAPIVersion();
  898.         }
  899.     }
  900.  
  901.     // }}}
  902.     // {{{ getVersion()
  903.  
  904.     
  905.  
  906.     /**
  907.      * Get the subclass version.
  908.      *
  909.      * @returns string Version.
  910.      * @access protected
  911.      */
  912.     function getVersion()
  913.     {
  914.         return $this->version;
  915.     }
  916.  
  917.     // }}}
  918.  
  919. }
  920.  
  921. // }}}
  922. /*
  923.  * Local variables:
  924.  * mode: php
  925.  * tab-width: 4
  926.  * c-basic-offset: 4
  927.  * c-hanging-comment-ender-p: nil
  928.  * End:
  929.  */
  930. ?>

Documentation generated on Thu, 23 Nov 2006 18:00:10 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.