Source for file Graphics.php
Documentation is available at Graphics.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
// +----------------------------------------------------------------------+
// $Id: Graphics.php,v 1.7 2005/01/26 09:47:28 dufuz Exp $
require_once 'Cache.php';
* The usual way to create images is to pass some arguments that describe the image
* to a script that dynamically creates an image. For every image of a page
* a new PHP interpreter gets started. This is a good way to kill your webserver.
* When dealing with dynamically generated images you should not call another script
* to generate the images but generate the images by the script that produces the page
* that contains the images. This is a major improvement but it's only half the way.
* There's no need to rerender an image on every request. A simple disk cache can reduce
* the computation time dramatically. This is what the class graphics_cache is for.
* // create an instance of the graphics cache
* $cache = new graphics_cache;
* $img = ImageCreate(...);
* // compute an ID for your image based on typical parameters
* $id = m5d( $size, $colors, $label);
* // check if it's cached
* if (!($link = $cache->getImageLink($id, 'gif'))) {
* // hmmm, it's not cached, create it
* // cacheImageLink() and cacheImage() make the ImageGIF() call!
* // cacheImage() returns the value of ImageGIF() [etc.], cacheImageLink() returns a URL
* $link = $cache->cacheImageLink($id, $img, 'gif');
* // Ok, let's build the ImageLink
* $size = getImageSize($link[0]);
* printf('<img src="%s" %s>', $link[1], $size[3]);
* // header('Content-type: image/gif'); print $cache->cacheImage($id, $img, 'gif');
* The class requires PHP 4.0.2+ [ImageType()]. Note that cacheImage() works with
* the output buffer. Modify it if required!
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @version $Id: Graphics.php,v 1.7 2005/01/26 09:47:28 dufuz Exp $
* Make sure that the cache URL prefix points to the $cache_dir, otherwise
* your links will be broken. Use setCacheURL to specify the cache_url and
* setCacheDir() for the cache_dir.
* @see setCacheURL(), setCacheDir()
* Directory where cached files get stored.
* Make sure that the cache_dir is writable and offers enough space. Check
* also if your cache_url points to the directory. Use setCacheDir() to set
* @see setCacheDir(), setCacheURL()
* Nameprefix of cached files.
* Per default the prefix "graphics_" gets used. You might use this
* for versioning or to ease (manual) clean ups.
* Mapping from supported image type to a ImageType() constant.
* Referr to the PHP manual for more informations on ImageType()
* @link http://www.php.net/ImageType
* Instantiates a cache file container.
* Returns the content of a cached image file.
* This function can be used to send the image directly to the browser.
* Make sure that you send a correspondending header before sending the image itself.
* Always try to get the image from the cache before you compute it. See
* the class docs for an example.
* @param string Image type: gif, jpg, png, wbmp
* @return string Image file contents if a cached file exists otherwise an empty string
* Returns an array with a link to the cached image and the image file path.
* Always try to get the image from the cache before you compute it. See
* the class docs for an example.
* @param string Image type: gif, jpg, png, wbmp
* @return array [ full path to the image file, image url ]
} // end func getImageLink
* Create an image from the given image handler, cache it and return the file content.
* Always try to retrive the image from the cache before you compute it.
* Warning: this function uses the output buffer. If you expect collisions
* @param string Image-ID. Used as a part of the cache filename.
* Use md5() to generate a "unique" ID for your image
* based on characteristic values such as the color, size etc.
* @param string Image handler to create the image from.
* @param string Image type: gif, jpg, png, wbmp. Also used as filename suffix.
* If an unsupported type is requested the functions tries to
* fallback to a supported type before throwing an exeption.
* @return string Image content returned by ImageGIF/...
return new Cache_Error('You must provide an ID for and image to be cached!', __FILE__ , __LINE__ );
// Check if the requested image type is supported by the GD lib.
// If not, try a callback to the first available image type.
foreach ($this->imagetypes as $supported => $bitmask) {
new Cache_Error(" The build in GD lib does not support the image type $format. Fallback to $supported." , __FILE__ , __LINE__ );
return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types.", __FILE__ , __LINE__ );
// save the image to the output buffer, write it to disk and
$func = 'Image' . $genFormat;
// save the generated image to disk
* Create an image from the given image handler, cache it and return a url and the file path of the image.
* Always try to retrive the image from the cache before you compute it.
* @param string Image-ID. Used as a part of the cache filename.
* Use md5() to generate a "unique" ID for your image
* based on characteristic values such as the color, size etc.
* @param string Image handler to create the image from.
* @param string Image type: gif, jpg, png, wbmp. Also used as filename suffix.
* If an unsupported type is requested the functions tries to
* fallback to a supported type before throwing an exeption.
* @return array [ full path to the image file, image url ]
return new Cache_Error ('You must provide an ID for and image to be cached!', __FILE__ , __LINE__ );
// Check if the requested image type is supported by the GD lib.
// If not, try a callback to the first available image type.
foreach ($this->imagetypes as $supported => $bitmask)
new Cache_Error(" The build in GD lib does not support the image type $format. Fallback to $supported." , __FILE__ , __LINE__ );
return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types.", __FILE__ , __LINE__ );
return array ($ffile, $url);
$func = 'Image' . $genFormat;
return array ($ffile, $url);
} // end func cacheImageLink
* Sets the URL prefix used when rendering HTML Tags.
* Make sure that the URL matches the cache directory,
* otherwise you'll get broken links.
if ($cache_url && '/' != substr($cache_url, 1 )) {
} // end func setCacheURL
* Sets the directory where to cache generated Images
if ($cache_dir && '/' != substr($cache_dir, 1 )) {
} // end func setCacheDir
} // end class Cache_Graphics
Documentation generated on Mon, 11 Mar 2019 15:25:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|