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-2006 Tobias Schlitt <toby@php.net>,
* Firman Wandayandi <firman@php.net>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the BSD License.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @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 CVS: $Id: Tools.php,v 1.7 2006/11/23 22:17:31 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: 0.4.1
* 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 input given.');
// {{{ 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.
* 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',
* @return resource The GD image resource of rendered result.
* Display rendered image (send it to browser).
* This method is a common implementation to render and output an image.
* The method calls the render() method automatically and outputs the
* @param int $type optional Type of format image (use defined image
* type constants), default is IMAGETYPE_PNG.
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
* @see IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED,
* IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
function display($type = IMAGETYPE_PNG )
if (PEAR ::isError ($res)) {
return PEAR ::raiseError ('Error rendering: ' . $res->getMessage ());
header('Content-Type: ' . $ctype);
return PEAR ::raiseError ('Headers have already been sent. Could not display image.',
return PEAR ::raiseError ('Image type '. $ctype. ' not supported by PHP',
* Save rendered image to a file.
* @param string $path Path to destination filename.
* @param int $type optional Type of image format (use defined image type
* constants), default is IMAGETYPE_PNG.
* @return bool|PEAR_ErrorTRUE on success, PEAR_Error on failure.
* @see IMAGE_TOOLS_BASE_ERR_HEADERSEND_FAILED,
IMAGE_TOOLS_BASE_ERR_IMAGETYPE_UNSUPPORTED
function save($path, $type = IMAGETYPE_PNG )
if (PEAR ::isError ($res)) {
return PEAR ::raiseError ('Error rendering: ' . $resultImage->getMessage ());
return PEAR ::raiseError ('Image type '. $ctype. ' not supported by PHP',
PEAR ::raiseError ('Saving image '. $path. ' failed',
* Find out the whether a subclass supports the option
* @param string $option Option name.
* @return bool TRUE if supported, otherwise FALSE
* @see IMAGE_TOOLS_BASE_ERR_OPTION_UNSUPPORTED,
* IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
return PEAR ::raiseError ('Unsupported option '. $option,
// {{{ availableOptions()
* Get all available options of the subclass
* @return array Available options.
* @see IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
* Get the method parameters
* @param string $methodName Method name.
* @return array|boolarray of the parameters a method expects or
* FALSE if the method is not available.
* @see IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
return PEAR ::raiseError ('Unsupported method '. $methodName,
// {{{ availableMethods()
* returns an array where the keys are the method names available
* and the value for each key is an array containing the
* parameter the specific method expects.
* @return array Available methods.
* @see IMAGE_TOOLS_BASE_ERR_STATIC_FAILED
return PEAR ::raiseError ('This method may only be called non statically',
* Get the API version of the common base
* This methods can be called statically using Image_Tools::getAPIVersion() or
* from the subclass e.g Image_Tools_Border::getAPIVersion() or
* $border->getAPIVersion()
* @return string Image_Tools base class api-version
return $obj->getAPIVersion ();
* Get the subclass version.
* @returns string Version.
* c-hanging-comment-ender-p: nil
Documentation generated on Thu, 23 Nov 2006 18:00:10 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|