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

Source for file Graphics.php

Documentation is available at Graphics.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Graphics.php,v 1.5 2003/01/04 11:54:45 mj Exp $
  19.  
  20. require_once 'Cache.php';
  21.  
  22. /**
  23. * Graphics disk cache.
  24. * The usual way to create images is to pass some arguments that describe the image
  25. * to a script that dynamically creates an image. For every image of a page
  26. * a new PHP interpreter gets started. This is a good way to kill your webserver.
  27. * When dealing with dynamically generated images you should not call another script
  28. * to generate the images but generate the images by the script that produces the page
  29. * that contains the images. This is a major improvement but it's only half the way.
  30. * There's no need to rerender an image on every request. A simple disk cache can reduce
  31. * the computation time dramatically. This is what the class graphics_cache is for.
  32. * Usage:
  33. * // create an instance of the graphics cache
  34. * $cache = new graphics_cache;
  35. *
  36. * $img = ImageCreate(...);
  37. *
  38. * // compute an ID for your image based on typical parameters
  39. * $id = m5d( $size, $colors, $label);
  40. * // check if it's cached
  41. * if (!($link = $cache->getImageLink($id, 'gif'))) {
  42. *  
  43. *   // hmmm, it's not cached, create it
  44. *   ...
  45. *   // cacheImageLink() and cacheImage() make the ImageGIF() call!
  46. *   // cacheImage() returns the value of ImageGIF() [etc.], cacheImageLink() returns a URL
  47. *   $link = $cache->cacheImageLink($id, $img, 'gif');
  48. * }
  49. *
  50. * // Ok, let's build the ImageLink
  51. * $size = getImageSize($link[0]);
  52. * printf('<img src="%s" %s>', $link[1], $size[3]);
  53. *
  54. * // for cacheImage():
  55. * // header('Content-type: image/gif'); print $cache->cacheImage($id, $img, 'gif');
  56. *
  57. * The class requires PHP 4.0.2+ [ImageType()]. Note that cacheImage() works with
  58. * the output buffer. Modify it if required!
  59. *
  60. @author   Ulf Wendel <ulf.wendel@phpdoc.de>
  61. @version  $Id: Graphics.php,v 1.5 2003/01/04 11:54:45 mj Exp $
  62. @package  Cache
  63. */
  64. class Cache_Graphics extends Cache {
  65.  
  66.  
  67.     /**
  68.     * Cache URL prefix.
  69.     * 
  70.     * Make sure that the cache URL prefix points to the $cache_dir, otherwise
  71.     * your links will be broken. Use setCacheURL to specify the cache_url and
  72.     * setCacheDir() for the cache_dir.
  73.     * 
  74.     * @var  string 
  75.     * @see  setCacheURL(), setCacheDir()
  76.     */
  77.     var $cache_url = '';
  78.  
  79.     /**
  80.     * Directory where cached files get stored.
  81.     * s
  82.     * Make sure that the cache_dir is writable and offers enough space. Check
  83.     * also if your cache_url points to the directory. Use setCacheDir() to set
  84.     * the variable.
  85.     * 
  86.     * @var  string 
  87.     * @see  setCacheDir(), setCacheURL()
  88.     */
  89.     var $cache_dir = '';
  90.  
  91.     /**
  92.     * Nameprefix of cached files.
  93.     * 
  94.     * Per default the prefix "graphics_" gets used. You might use this
  95.     * for versioning or to ease (manual) clean ups.
  96.     *
  97.     * @var      string 
  98.     */
  99.     var $cache_file_prefix = 'graphics_';
  100.     
  101.     
  102.     /**
  103.     * Cache container group.
  104.     *
  105.     * @var      string 
  106.     */
  107.     var $cache_group = 'graphics';
  108.  
  109.     
  110.     /**
  111.     * Mapping from supported image type to a ImageType() constant.
  112.     * 
  113.     * Referr to the PHP manual for more informations on ImageType()
  114.     * 
  115.     * @var  array 
  116.     * @link http://www.php.net/ImageType
  117.     */
  118.     var $imagetypes = array(
  119.                                 'gif'   => IMG_GIF
  120.                                 'jpg'   => IMG_JPG,
  121.                                 'png'   => IMG_PNG,
  122.                                 'wbmp'  => IMG_WBMP
  123.                             );
  124.  
  125.                             
  126.     /**
  127.     * Instantiates a cache file container.
  128.     *
  129.     */
  130.     function Cache_Graphics({
  131.     
  132.         $this->Cache('file'array('cache_dir' => $this->cache_dir'filename_prefix' => $this->cache_file_prefix));
  133.         
  134.     // end constructor
  135.  
  136.     
  137.     /**
  138.     * Returns the content of a cached image file.
  139.     * 
  140.     * This function can be used to send the image directly to the browser.
  141.     * Make sure that you send a correspondending header before sending the image itself.
  142.     *
  143.     * Always try to get the image from the cache before you compute it. See
  144.     * the class docs for an example.
  145.     *
  146.     * @param    string  Image-ID
  147.     * @param    string  Image type: gif, jpg, png, wbmp
  148.     * @return   string  Image file contents if a cached file exists otherwise an empty string
  149.     * @see      cacheImage()
  150.     */                                    
  151.     function getImage($id$format 'png'{
  152.         $id $this->generateID($id$format);
  153.         
  154.         return $this->get($id$this->cache_group);
  155.     // end func getImage
  156.  
  157.     
  158.     /**
  159.     * Returns an array with a link to the cached image and the image file path.
  160.     * 
  161.     * Always try to get the image from the cache before you compute it. See
  162.     * the class docs for an example.
  163.     *
  164.     * @param    string  Image-ID
  165.     * @param    string  Image type: gif, jpg, png, wbmp
  166.     * @return   array   [ full path to the image file, image url ]
  167.     * @throw    Cache_Error
  168.     * @see      cacheImageLink()
  169.     */
  170.     function getImageLink($id$format 'png'{
  171.         $id $this->generateID($id$format);
  172.         if (!$this->container->idExists($id$this->cache_group)) 
  173.             return array();
  174.  
  175.         $file $this->cache_url . $this->cache_file_prefix . $id;
  176.  
  177.         return array($this->container->getFilename($id$this->cache_group)$file);
  178.     // end func getImageLink
  179.     
  180.  
  181.     /**
  182.     * Create an image from the given image handler, cache it and return the file content.
  183.     *
  184.     * Always try to retrive the image from the cache before you compute it.
  185.     * 
  186.     * Warning: this function uses the output buffer. If you expect collisions
  187.     * modify the code.
  188.     *
  189.     * @param    string  Image-ID. Used as a part of the cache filename.
  190.     *                    Use md5() to generate a "unique" ID for your image
  191.     *                    based on characteristic values such as the color, size etc.
  192.     * @param    string  Image handler to create the image from.
  193.     * @param    string  Image type: gif, jpg, png, wbmp. Also used as filename suffix.
  194.     *                    If an unsupported type is requested the functions tries to
  195.     *                    fallback to a supported type before throwing an exeption.
  196.     * @return   string  Image content returned by ImageGIF/...
  197.     * @throws   Cache_Error
  198.     * @access   public
  199.     * @see      getImage()
  200.     */
  201.     function cacheImage($id$img$format 'png'{
  202.         if (!$id)
  203.             return new Cache_Error('You must provide an ID for and image to be cached!'__FILE____LINE__);
  204.  
  205.         $id $this->generateID($id$format);
  206.         $types = ImageTypes();
  207.  
  208.         // Check if the requested image type is supported by the GD lib.
  209.         // If not, try a callback to the first available image type.
  210.         if (!isset($this->imagetypes[$format]|| !($types $this->imagetypes[$format])) {
  211.             foreach ($this->imagetypes as $supported => $bitmask{
  212.                 if ($types $bitmask{
  213.                     new Cache_Error("The build in GD lib does not support the image type $format. Fallback to $supported."__FILE____LINE__);
  214.                 else {
  215.                     return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types."__FILE____LINE__);
  216.                 }
  217.             }
  218.         }
  219.  
  220.         if ($image $this->get($id$this->cache_group))
  221.             return $image;
  222.  
  223.         // save the image to the output buffer, write it to disk and 
  224.         // return the image.
  225.         ob_end_clean();
  226.         ob_start()
  227.  
  228.         if (strtoupper($format== "JPG"{
  229.             $genFormat "JPEG";
  230.         else {
  231.             $genFormat strtoupper($format);
  232.         }
  233.  
  234.         // generate the image
  235.         $func 'Image' $genFormat;
  236.         $func($img);
  237.         ImageDestroy($img);
  238.  
  239.         ob_end();
  240.         $image ob_get_contents();
  241.         ob_end_clean();
  242.  
  243.         // save the generated image to disk
  244.         $this->save($id$image0$this->cache_group);
  245.  
  246.         return $image;
  247.     // end func cacheImage
  248.     
  249.  
  250.     /**
  251.     * Create an image from the given image handler, cache it and return a url and the file path of the image.
  252.     *
  253.     * Always try to retrive the image from the cache before you compute it.
  254.     *
  255.     * @param    string  Image-ID. Used as a part of the cache filename.
  256.     *                    Use md5() to generate a "unique" ID for your image
  257.     *                    based on characteristic values such as the color, size etc.
  258.     * @param    string  Image handler to create the image from.
  259.     * @param    string  Image type: gif, jpg, png, wbmp. Also used as filename suffix.
  260.     *                    If an unsupported type is requested the functions tries to
  261.     *                    fallback to a supported type before throwing an exeption.
  262.     * @return   array  [ full path to the image file, image url ]
  263.     * @throws   Cache_Error
  264.     * @access   public
  265.     */
  266.     function cacheImageLink($id&$img$format 'png'{
  267.         if (!$id)
  268.             return new Cache_Error ('You must provide an ID for and image to be cached!'__FILE____LINE__);
  269.  
  270.         $id $this->generateID($id$format);
  271.         $types = ImageTypes();
  272.  
  273.         // Check if the requested image type is supported by the GD lib.
  274.         // If not, try a callback to the first available image type.
  275.         if (!isset($this->imagetypes[$format]|| !($types $this->imagetypes[$format])) {
  276.             foreach ($this->imagetypes as $supported => $bitmask
  277.                 if ($types $bitmask)
  278.                     new Cache_Error("The build in GD lib does not support the image type $format. Fallback to $supported."__FILE____LINE__);
  279.                 else
  280.                     return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types."__FILE____LINE__);
  281.         }
  282.  
  283.         $url $this->cache_url . $this->cache_file_prefix . $id;
  284.         $ffile $this->container->getFilename($id$this->cache_group);
  285.  
  286.         if ($this->isCached($id$this->cache_group&& !isExpired($id$this->cache_group))
  287.             return array($ffile$url);
  288.  
  289.         if (strtoupper($format== "JPG"{
  290.             $genFormat "JPEG";
  291.         else {
  292.             $genFormat strtoupper($format);
  293.         }
  294.  
  295.         $func 'Image' $genFormat;
  296.         $func($img$ffile);
  297.  
  298.         ImageDestroy($img);
  299.  
  300.         return array($ffile$url);
  301.     // end func cacheImageLink
  302.  
  303.     
  304.     /**
  305.     * Sets the URL prefix used when rendering HTML Tags.
  306.     * 
  307.     * Make sure that the URL matches the cache directory,
  308.     * otherwise you'll get broken links.
  309.     * 
  310.     * @param    string 
  311.     * @access   public
  312.     * @see      setCacheDir()
  313.     */
  314.     function setCacheURL($cache_url{
  315.         if ($cache_url && '/' != substr($cache_url1)) 
  316.             $cache_url .= '/';
  317.             
  318.         $this->cache_url = $cache_url;
  319.         
  320.     // end func setCacheURL
  321.  
  322.     
  323.     /**
  324.     * Sets the directory where to cache generated Images
  325.     * 
  326.     * @param    string 
  327.     * @access   public
  328.     * @see      setCacheURL()
  329.     */
  330.     function setCacheDir($cache_dir{
  331.         if ($cache_dir && '/' != substr($cache_dir1))
  332.             $cache_dir .= '/';
  333.  
  334.         $this->cache_dir = $cache_dir;
  335.         $this->container->cache_dir = $cache_dir;
  336.     // end func setCacheDir
  337.     
  338.     
  339.     function generateID($variable$format 'png'{
  340.       return md5(serialize($variable)) '.' $format;
  341.     // end func generateID
  342.     
  343.     
  344. // end class Cache_Graphics
  345. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.