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 315102 2011-08-17 19:38:20Z cweiske $
  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 315102 2011-08-17 19:38:20Z cweiske $
  62. @package  Cache
  63. */
  64. class Cache_Graphics extends Cache
  65. {
  66.  
  67.  
  68.     /**
  69.     * Cache URL prefix.
  70.     * 
  71.     * Make sure that the cache URL prefix points to the $cache_dir, otherwise
  72.     * your links will be broken. Use setCacheURL to specify the cache_url and
  73.     * setCacheDir() for the cache_dir.
  74.     * 
  75.     * @var  string 
  76.     * @see  setCacheURL(), setCacheDir()
  77.     */
  78.     var $cache_url = '';
  79.  
  80.     /**
  81.     * Directory where cached files get stored.
  82.     * s
  83.     * Make sure that the cache_dir is writable and offers enough space. Check
  84.     * also if your cache_url points to the directory. Use setCacheDir() to set
  85.     * the variable.
  86.     * 
  87.     * @var  string 
  88.     * @see  setCacheDir(), setCacheURL()
  89.     */
  90.     var $cache_dir = '';
  91.  
  92.     /**
  93.     * Nameprefix of cached files.
  94.     * 
  95.     * Per default the prefix "graphics_" gets used. You might use this
  96.     * for versioning or to ease (manual) clean ups.
  97.     *
  98.     * @var      string 
  99.     */
  100.     var $cache_file_prefix = 'graphics_';
  101.     
  102.     
  103.     /**
  104.     * Cache container group.
  105.     *
  106.     * @var      string 
  107.     */
  108.     var $cache_group = 'graphics';
  109.  
  110.     
  111.     /**
  112.     * Mapping from supported image type to a ImageType() constant.
  113.     * 
  114.     * Referr to the PHP manual for more informations on ImageType()
  115.     * 
  116.     * @var  array 
  117.     * @link http://www.php.net/ImageType
  118.     */
  119.     var $imagetypes = array(
  120.                                 'gif'   => IMG_GIF
  121.                                 'jpg'   => IMG_JPG,
  122.                                 'png'   => IMG_PNG,
  123.                                 'wbmp'  => IMG_WBMP
  124.                             );
  125.  
  126.                             
  127.     /**
  128.     * Instantiates a cache file container.
  129.     *
  130.     */
  131.     function Cache_Graphics()
  132.     {
  133.         $this->Cache('file'array('cache_dir' => $this->cache_dir'filename_prefix' => $this->cache_file_prefix));
  134.         
  135.     // end constructor
  136.  
  137.     
  138.     /**
  139.     * Returns the content of a cached image file.
  140.     * 
  141.     * This function can be used to send the image directly to the browser.
  142.     * Make sure that you send a correspondending header before sending the image itself.
  143.     *
  144.     * Always try to get the image from the cache before you compute it. See
  145.     * the class docs for an example.
  146.     *
  147.     * @param    string  Image-ID
  148.     * @param    string  Image type: gif, jpg, png, wbmp
  149.     * @return   string  Image file contents if a cached file exists otherwise an empty string
  150.     * @see      cacheImage()
  151.     */                                    
  152.     function getImage($id$format 'png')
  153.     {
  154.         $id $this->generateID($id$format);
  155.         return $this->get($id$this->cache_group);
  156.     // end func getImage
  157.  
  158.     
  159.     /**
  160.     * Returns an array with a link to the cached image and the image file path.
  161.     * 
  162.     * Always try to get the image from the cache before you compute it. See
  163.     * the class docs for an example.
  164.     *
  165.     * @param    string  Image-ID
  166.     * @param    string  Image type: gif, jpg, png, wbmp
  167.     * @return   array   [ full path to the image file, image url ]
  168.     * @throw    Cache_Error
  169.     * @see      cacheImageLink()
  170.     */
  171.     function getImageLink($id$format 'png')
  172.     {
  173.         $id $this->generateID($id$format);
  174.         if (!$this->container->idExists($id$this->cache_group)) {
  175.             return array();
  176.         }
  177.         $file $this->cache_url . $this->cache_file_prefix . $id;
  178.  
  179.         return array($this->container->getFilename($id$this->cache_group)$file);
  180.     // end func getImageLink
  181.     
  182.  
  183.     /**
  184.     * Create an image from the given image handler, cache it and return the file content.
  185.     *
  186.     * Always try to retrive the image from the cache before you compute it.
  187.     * 
  188.     * Warning: this function uses the output buffer. If you expect collisions
  189.     * modify the code.
  190.     *
  191.     * @param    string  Image-ID. Used as a part of the cache filename.
  192.     *                    Use md5() to generate a "unique" ID for your image
  193.     *                    based on characteristic values such as the color, size etc.
  194.     * @param    string  Image handler to create the image from.
  195.     * @param    string  Image type: gif, jpg, png, wbmp. Also used as filename suffix.
  196.     *                    If an unsupported type is requested the functions tries to
  197.     *                    fallback to a supported type before throwing an exeption.
  198.     * @return   string  Image content returned by ImageGIF/...
  199.     * @throws   Cache_Error
  200.     * @access   public
  201.     * @see      getImage()
  202.     */
  203.     function cacheImage($id$img$format 'png')
  204.     {
  205.         if (!$id{
  206.             return new Cache_Error('You must provide an ID for and image to be cached!'__FILE____LINE__);
  207.         }
  208.         $id $this->generateID($id$format);
  209.         $types = ImageTypes();
  210.  
  211.         // Check if the requested image type is supported by the GD lib.
  212.         // If not, try a callback to the first available image type.
  213.         if (!isset($this->imagetypes[$format]|| !($types $this->imagetypes[$format])) {
  214.             foreach ($this->imagetypes as $supported => $bitmask{
  215.                 if ($types $bitmask{
  216.                     new Cache_Error("The build in GD lib does not support the image type $format. Fallback to $supported."__FILE____LINE__);
  217.                 else {
  218.                     return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types."__FILE____LINE__);
  219.                 }
  220.             }
  221.         }
  222.  
  223.         if ($image $this->get($id$this->cache_group)) {
  224.             return $image;
  225.         }
  226.         // save the image to the output buffer, write it to disk and 
  227.         // return the image.
  228.         ob_end_clean();
  229.         ob_start()
  230.  
  231.         if (strtoupper($format== 'JPG'{
  232.             $genFormat 'JPEG';
  233.         else {
  234.             $genFormat strtoupper($format);
  235.         }
  236.  
  237.         // generate the image
  238.         $func 'Image' $genFormat;
  239.         $func($img);
  240.         ImageDestroy($img);
  241.  
  242.         ob_end();
  243.         $image ob_get_contents();
  244.         ob_end_clean();
  245.  
  246.         // save the generated image to disk
  247.         $this->save($id$image0$this->cache_group);
  248.  
  249.         return $image;
  250.     // end func cacheImage
  251.     
  252.  
  253.     /**
  254.     * Create an image from the given image handler, cache it and return a url and the file path of the image.
  255.     *
  256.     * Always try to retrive the image from the cache before you compute it.
  257.     *
  258.     * @param    string  Image-ID. Used as a part of the cache filename.
  259.     *                    Use md5() to generate a "unique" ID for your image
  260.     *                    based on characteristic values such as the color, size etc.
  261.     * @param    string  Image handler to create the image from.
  262.     * @param    string  Image type: gif, jpg, png, wbmp. Also used as filename suffix.
  263.     *                    If an unsupported type is requested the functions tries to
  264.     *                    fallback to a supported type before throwing an exeption.
  265.     * @return   array  [ full path to the image file, image url ]
  266.     * @throws   Cache_Error
  267.     * @access   public
  268.     */
  269.     function cacheImageLink($id&$img$format 'png')
  270.     {
  271.         if (!$id{
  272.             return new Cache_Error ('You must provide an ID for and image to be cached!'__FILE____LINE__);
  273.          }
  274.         $id $this->generateID($id$format);
  275.         $types = ImageTypes();
  276.  
  277.         // Check if the requested image type is supported by the GD lib.
  278.         // If not, try a callback to the first available image type.
  279.         if (!isset($this->imagetypes[$format]|| !($types $this->imagetypes[$format])) {
  280.             foreach ($this->imagetypes as $supported => $bitmask
  281.                 if ($types $bitmask{
  282.                     new Cache_Error("The build in GD lib does not support the image type $format. Fallback to $supported."__FILE____LINE__);
  283.                 else {
  284.                     return new Cache_Error("Hmm, is your PHP build with GD support? Can't find any supported types."__FILE____LINE__);
  285.                 }
  286.         }
  287.  
  288.         $url $this->cache_url . $this->cache_file_prefix . $id;
  289.         $ffile $this->container->getFilename($id$this->cache_group);
  290.  
  291.         if ($this->isCached($id$this->cache_group&& !isExpired($id$this->cache_group)) {
  292.             return array($ffile$url);
  293.         }
  294.  
  295.         if (strtoupper($format== 'JPG'{
  296.             $genFormat 'JPEG';
  297.         else {
  298.             $genFormat strtoupper($format);
  299.         }
  300.  
  301.         $func 'Image' $genFormat;
  302.         $func($img$ffile);
  303.  
  304.         ImageDestroy($img);
  305.  
  306.         return array($ffile$url);
  307.     // end func cacheImageLink
  308.  
  309.     
  310.     /**
  311.     * Sets the URL prefix used when rendering HTML Tags.
  312.     * 
  313.     * Make sure that the URL matches the cache directory,
  314.     * otherwise you'll get broken links.
  315.     * 
  316.     * @param    string 
  317.     * @access   public
  318.     * @see      setCacheDir()
  319.     */
  320.     function setCacheURL($cache_url)
  321.     {
  322.         if ($cache_url && '/' != substr($cache_url1)) {
  323.             $cache_url .= '/';
  324.         }
  325.         $this->cache_url = $cache_url;
  326.         
  327.     // end func setCacheURL
  328.  
  329.     
  330.     /**
  331.     * Sets the directory where to cache generated Images
  332.     * 
  333.     * @param    string 
  334.     * @access   public
  335.     * @see      setCacheURL()
  336.     */
  337.     function setCacheDir($cache_dir)
  338.     {
  339.         if ($cache_dir && '/' != substr($cache_dir1)) {
  340.             $cache_dir .= '/';
  341.         }
  342.         $this->cache_dir = $cache_dir;
  343.         $this->container->cache_dir = $cache_dir;
  344.     // end func setCacheDir
  345.     
  346.     
  347.     function generateID($variable$format 'png')
  348.     {
  349.       return md5(serialize($variable)) '.' $format;
  350.     // end func generateID
  351.     
  352.     
  353. // end class Cache_Graphics
  354. ?>

Documentation generated on Mon, 11 Mar 2019 15:44:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.