Source for file Tools.php
Documentation is available at Tools.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
* This is a main file of Image_Tools package.
* Copyright (c) 2005-2008
* Tobias Schlitt <toby@php.net>,
* Firman Wandayandi <firman@php.net>
* This source file is subject to the BSD License license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to pear-dev@list.php.net so we can send you a copy immediately.
* @author Tobias Schlitt <toby@php.net>
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2003-2008
* Tobias Schlitt <toby@php.net>,
* Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version CVS: $Id: Tools.php,v 1.13 2008/05/26 09:18:09 firman Exp $
* load PEAR for error handling.
* load PHP_Compat for PHP backward compatibility.
require_once 'PHP/Compat.php';
* is_a(), since PHP 4.2.0
@PHP_Compat ::loadFunction ('is_a');
* image_type_to_mime_type, since PHP 4.3.0
@PHP_Compat ::loadFunction ('image_type_to_mime_type');
* Image_Tools error, indicating that the given tool was not found.
* @name IMAGE_TOOLS_BASE_ERR_CLASS_INVALID
define('IMAGE_TOOLS_BASE_ERR_CLASS_INVALID', -1 );
* Image_Tools error, indicating that the given tool object could not be
* @name IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED
define('IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED', -2 );
* Image_Tools error, indicating that you may not instanciate
* the base class Image_Tools directly.
* @name IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED
define('IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FORBIDEN', -3 );
* Image_Tools error, indicating that the given option was of a wrong type.
* @name IMAGE_TOOLS_BASE_ERR_OPTION_INVALID
define('IMAGE_TOOLS_BASE_ERR_OPTION_INVALID', -4 );
* Image_Tools error, indicating that the given option is not
* @name IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
define('IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED', -5 );
* Image_Tools error, indicating that the given method is not
* @name IMAGE_TOOLS_BASE_ERR_METHOD_UNSUPPORTED
define('IMAGE_TOOLS_BASE_ERR_METHOD_UNSUPPORTED', -6 );
* Image_Tools error, indicating that the given option is not
* @name IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET
define('IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET', -7 );
* Image_Tools error, indicating that the HTTP headers have been sent
* before the display() method was called. Ensure that no output started
* @name IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED
define('IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED', -8 );
* Image_Tools error, indicating that the image type you selected for
* displaying the image is not supported. Please use a supported image
* @name IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
define('IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED', -9 );
* Image_Tools error, indicating that the image could not be saved.
* Check the permissions on the desired path first.
* @name IMAGE_TOOLS_BASE_ERR_SAVEIMAGE_FAILED
define('IMAGE_TOOLS_BASE_ERR_SAVEIMAGE_FAILED', -10 );
* Image_Tools error, indicating that you called a static method non
* statically, which may only be called statically.
* @name IMAGE_TOOLS_BASE_ERR_NONSTATIC_FAILED
define('IMAGE_TOOLS_BASE_ERR_NONSTATIC_FAILED', -11 );
* Image_Tools error, indicating that you called a static method
* statically, which may only be called non statically.
* @name IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
define('IMAGE_TOOLS_BASE_ERR_STATIC_FAILED', -12 );
// {{{ Class: Image_Tools
* This is the Image_Tools base class
* Every image-tool has to derive from this class and implement several methods
* itself. Other methods are implemented in the base class directly, but may
* only be called on the sub-classes. The only method being called directly from
* the base class is the factory() method.
* @author Tobias Schlitt <toby@php.net>
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2003-2006
* Tobias Schlitt <toby@php.net>,
* Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version Release: 1.0.0RC1
* Contain the options inside all subclasses.
* Has to contain all available options of a subclass. The index of
* the array is the option name, the value is either a PHP variable
* type (e.g. int, string, float, resource,...) or a class name
* (e.g. Image_Tools, DB,...). Also allowed the are the special values
* 'number' and 'mixed'. The structure of that array is as follows:
* $availableOptions = array(
* Has to contain all available methods (as the keys of the array)
* and the expected parameters (in an array) as the values. The structure
* of this array is as follows:
* $availableMethods = array(
* Contains the version of the specific subclass. A version has to follow the PEAR
* version naming standard, which can be found here:
* http://pear.php.net/group/docs/20040226-vn.php
* Contains the api-version of the baseclass. The API version has to follow the
* PEAR version naming standard.
* this method has no purpose for Image_Tools, but defines the API for
* all base classes. Calling this directly with $foo = new Image_Tools(...);
* @param array $options optional Options.
* @see IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FORBIDEN
PEAR ::setErrorHandling (PEAR_ERROR_TRIGGER );
PEAR ::raiseError ('Cannot instanciate Image_Tools directly',
PEAR ::setErrorHandling (PEAR_ERROR_RETURN );
* Create a new instance of an image tool
* This method will create a new image tool, defined by the $tool var,
* which is a string it will call the constructor of the specific tool
* and return it (if no error occurs)
* @param string $tool Tool name
* @param array $options optional Options
* @return object New image tool on success or PEAR_Error on failure
* @see IMAGE_TOOLS_BASE_ERR_INSTANCIATION_FAILED,
* IMAGE_TOOLS_BASE_ERR_CLASS_INVALID,
* IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
* IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
function &factory($tool, $options = array ())
return PEAR ::raiseError ('This method may only be called statically',
if (empty ($toolParts) || !is_array($toolParts)) {
foreach ($toolParts as $key => $value) {
$toolParts[$key] = ucfirst($value);
$toolPath = 'Image/Tools/'. $tool. '.php';
// Include the tool class
$className = " Image_Tools_${tool}";
return PEAR ::raiseError ('File not found ' . $toolPath .
' or undefined class '. $className,
@$obj = & new $className($options);
return PEAR ::raiseError ('Could not instanciate image tool '. $className,
$res = $obj->set ($options);
if (PEAR ::isError ($res)) {
* Set a single or multiple options. The parameter of that method may
* either be a single options array or a key/value pair setting a single
* option. This method has not to be reimplemented by the Image_Tools
* @param mixed $option A single option name or the options array
* @param mixed $value Option value if $option is string
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure
* @see IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
* IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
function set($option, $value = null )
return PEAR ::raiseError ('This method may only be called non statically',
foreach ($option as $key => $value) {
$res = $this->set($key, @$value);
if (PEAR ::isError ($res)) {
if (PEAR ::isError ($res)) {
* Has the option a valid value?
* Determines, if the value given is valid for the option.
* @param mixed $option A single option name
* @param mixed $value Option value
* @return bool|PEAR_ErrorTRUE on acceptans or PEAR_Error on failure
* @see IMAGE_TOOLS_BASE_ERR_WRONGOPTIONTYPE,
* IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED
if (PEAR ::isError ($res)) {
$typeCheck = 'is_'. $type;
if ($typeCheck($value)) {
if (is_a($value, $type)) {
return PEAR ::raiseError ('Wrong type for option ' . $name .
'. Requires '. $type . ', but is ' . gettype($value),
* Get the value of the option
* returns the value of an option of, an error if the option is not
* @param string $option Option name.
* @return mixed|PEAR_ErrorOption value on success or
* @see IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED,
* IMAGE_TOOLS_BASE_ERR_OPTION_NOTSET,
* IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
if (PEAR ::isError ($res)) {
if (isset ($this->options[$option])) {
return PEAR ::raiseError ('Option '. $option. ' not set',
// {{{ createImageFromFile()
* Create a GD image resource from file (JPEG, PNG, WBMP and XBM support).
* @param string $filename The image filename.
* @return mixed GD image resource on success, PEAR_Error on failure.
return PEAR ::raiseError ('Unable to open file "' . $filename . '"');
// determine image format
return PEAR ::raiseError ('Unsupport image type');
// {{{ createImageFromString()
* Create a GD image resource from a string data.
* @param string $data The string image data.
* @return mixed GD image resource on success, PEAR_Error on failure.
PEAR ::raiseError ('Invalid data value.');
return PEAR ::raiseError ('Failed to create image from string data');
* Create a GD image resource from given input.
* This method tried to detect what the input, if it is a file the
* createImageFromFile will be called, otherwise createImageFromString().
* @param mixed $input The input for creating an image resource. The value
* may a string of filename, string of image data or
* @return resource|PEAR_ErrorAn GD image resource on success or
* @see createImageFromFile()
* @see createImageFromString()
return PEAR ::raiseError ('Invalid source image given, valid to create image resource.');
// {{{ isGDImageResource()
* Find the whether a value is the GD image resource or not.
* @param mixed $value Value to evaluate.
* @return bool TRUE if a value is the GD image resource, otherwise FALSE.
* Function which called before render.
* Use this method to place any routine before calling render(). This
* method is optional to extended and returns TRUE as default.
* @return true|PEAR_Error
* @since Method available since Release 1.0.0RC1
* Function which called after render.
* Use this method to place any routine after calling render(). This
* method is optional to extended and return TRUE as default.
* @return true|PEAR_Error
* @since Method available since Release 1.0.0RC1
* Render the result of a tool to the given image
* Since that's the purpose of all image tools, this method should stay
* with it's api as is. The rendering itself must be tool specific. The
* method gets a GD2 image as it's parameter. The creation of images inside
* Image_Tools is not supported.
* @see IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
* Helper function to combine calls between pre render, render and
* @return TRUE|PEAR_ErrorTRUE on success or PEAR_Error on failure.
* @since Method available since Release 1.0.0RC1
return PEAR ::raiseError ('This method may only be called non statically',
// calls the pre render routine
if (PEAR ::isError ($res)) {
// calls the extended render function
if (PEAR ::isError ($res)) {
|