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

Class: Cache_Graphics

Source Location: /Cache-1.5.7/Cache/Graphics.php

Class Overview

PEAR
   |
   --Cache
      |
      --Cache_Graphics

Graphics disk cache.


Author(s):

Version:

  • $Id: Graphics.php 315102 2011-08-17 19:38:20Z cweiske $

Variables

Methods


Inherited Variables

Inherited Methods

Class: Cache

Cache::Cache()
this Constructor set the Container Property as a Cache_Container|Cache_Error Object.
Cache::extSave()
Stores a dataset with additional userdefined data.
Cache::flush()
Flushes the cache - removes all data from it
Cache::garbageCollection()
Calls the garbage collector of the storage object with a certain probability
Cache::generateID()
Generates a "unique" ID for the given value
Cache::get()
Returns the requested dataset it if exists and is not expired
Cache::getCaching()
Returns the current caching state.
Cache::getUserdata()
Returns the userdata field of a cached data set.
Cache::isCached()
Checks if a dataset exists.
Cache::isExpired()
Checks if a dataset is expired
Cache::load()
Loads the given ID from the cache.
Cache::remove()
Removes the specified dataset from the cache.
Cache::save()
Stores the given data in the cache.
Cache::setCaching()
Enables or disables caching.
Cache::_Cache()

Class Details

[line 72]
Graphics disk cache.

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.

Usage:

// 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]);

// for cacheImage(): // 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 315102 2011-08-17 19:38:20Z cweiske $


[ Top ]


Class Variables

$cache_dir =  ''

[line 98]

Directory where cached files get stored.

s 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 the variable.


Type:   string


[ Top ]

$cache_file_prefix =  'graphics_'

[line 108]

Nameprefix of cached files.

Per default the prefix "graphics_" gets used. You might use this for versioning or to ease (manual) clean ups.


Type:   string


[ Top ]

$cache_group =  'graphics'

[line 116]

Cache container group.

Type:   string


[ Top ]

$cache_url =  ''

[line 86]

Cache URL prefix.

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.


Type:   string


[ Top ]

$imagetypes = array(
                                'gif'   => IMG_GIF, 
                                'jpg'   => IMG_JPG,
                                'png'   => IMG_PNG,
                                'wbmp'  => IMG_WBMP
                            )

[line 127]

Mapping from supported image type to a ImageType() constant.

Referr to the PHP manual for more informations on ImageType()


Type:   array


[ Top ]



Method Detail

Cache_Graphics (Constructor)   [line 139]

Cache_Graphics Cache_Graphics( )

Instantiates a cache file container.

[ Top ]

cacheImage   [line 211]

string cacheImage( string $id, string $img, [string $format = 'png'])

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 modify the code.


Parameters:

string   $id     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.
string   $img     Image handler to create the image from.
string   $format     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.

[ Top ]

cacheImageLink   [line 277]

array cacheImageLink( string $id, string &$img, [string $format = 'png'])

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.

  • Return: [ full path to the image file, image url ]
  • Throws: Cache_Error
  • Access: public

Parameters:

string   $id     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.
string   &$img     Image handler to create the image from.
string   $format     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.

[ Top ]

generateID   [line 355]

void generateID( $variable, [ $format = 'png'])


Overrides Cache::generateID() (Generates a "unique" ID for the given value)

Parameters:

   $variable     
   $format     

[ Top ]

getImage   [line 160]

string getImage( string $id, [string $format = 'png'])

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.


Parameters:

string   $id     Image-ID
string   $format     Image type: gif, jpg, png, wbmp

[ Top ]

getImageLink   [line 179]

array getImageLink( string $id, [string $format = 'png'])

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.


Parameters:

string   $id     Image-ID
string   $format     Image type: gif, jpg, png, wbmp

[ Top ]

setCacheDir   [line 345]

void setCacheDir( string $cache_dir)

Sets the directory where to cache generated Images

Parameters:

string   $cache_dir     

[ Top ]

setCacheURL   [line 328]

void setCacheURL( string $cache_url)

Sets the URL prefix used when rendering HTML Tags.

Make sure that the URL matches the cache directory, otherwise you'll get broken links.


Parameters:

string   $cache_url     

[ Top ]


Documentation generated on Thu, 15 Sep 2011 07:00:05 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.