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

Source for file Image.php

Documentation is available at Image.php

  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | PEAR :: XML :: Transformer :: Image Namespace Handler                     |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
  7. // +---------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,           |
  9. // | that is available at http://www.php.net/license/3_0.txt.                  |
  10. // | If you did not receive a copy of the PHP license and are unable to        |
  11. // | obtain it through the world-wide-web, please send a note to               |
  12. // | license@php.net so we can mail you a copy immediately.                    |
  13. // +---------------------------------------------------------------------------+
  14. //
  15. // $Id$
  16. //
  17.  
  18. require_once 'XML/Transformer/Namespace.php';
  19. require_once 'XML/Util.php';
  20.  
  21. define('PEAR_XML_TRANSFORMER_IMAGE_FONTPATH''/usr/X11R6/lib/X11/fonts/truetype');
  22. define('PEAR_XML_TRANSFORMER_IMAGE_cacheDir''/cache/gtext');
  23.  
  24. /**
  25.  * Handler for the Image Namespace.
  26.  *
  27.  * Example:
  28.  *
  29.  * <code>
  30.  * <?php
  31.  * require_once 'XML/Transformer_OutputBuffer.php';
  32.  * require_once 'XML/Transformer/Namespace/Image.php';
  33.  *
  34.  * $t = new XML_Transformer_OutputBuffer;
  35.  * $t->overloadNamespace('img', new XML_Transformer_Namespace_Image);
  36.  * $t->start();
  37.  * ?>
  38.  * <html>
  39.  *   <body>
  40.  *     <!-- Height and Width attributes are autogenerated -->
  41.  *     <img:img src="somepng.png" alt="A sample image" />
  42.  *
  43.  *     <!-- Set default for all subsequent <img:gtext /> -->
  44.  *     <img:gtextdefault bgcolor="888888" fgcolor="#000000"
  45.  *                       font="arial.ttf" fontsize="33"
  46.  *                       border="2" spacing="2"
  47.  *                       split="" cacheable="yes" />
  48.  *
  49.  *     <!-- Render Text as PNG image -->
  50.  *   </body>
  51.  * </html>
  52.  * </code>
  53.  *
  54.  * Output:
  55.  *
  56.  * <code>
  57.  * <html>
  58.  *   <body>
  59.  *     <img alt="A sample image" height="33" src="somepng.png" width="133" />
  60.  *                src="/cache/gtext/8b91aee0403c5cdccc1dd96bd4f49fbb.png" /></span>
  61.  *   </body>
  62.  * </html>
  63.  * </code>
  64.  *
  65.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  66.  * @author
  67.  * @copyright
  68.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  69.  * @category    XML
  70.  * @package     XML_Transformer
  71.  */
  72.     // {{{ Members
  73.  
  74.     /**
  75.     * @var    boolean 
  76.     * @access public
  77.     */
  78.     var $defaultNamespacePrefix = 'img';
  79.  
  80.     /**
  81.     * @var    array 
  82.     * @access private
  83.     */
  84.     var $_imgAttributes          = array();
  85.  
  86.     /**
  87.     * @var    array 
  88.     * @access private
  89.     */
  90.     var $_gtextAttributes        = array();
  91.  
  92.     /**
  93.     * @var    array 
  94.     * @access private
  95.     */
  96.     var $_gtextDefaultAttributes = array();
  97.  
  98.     // }}}
  99.     // {{{ function start_img($attributes)
  100.  
  101.     /**
  102.     * @param  array 
  103.     * @return string 
  104.     * @access public
  105.     */
  106.     function start_img($attributes{
  107.         $this->_imgAttributes $attributes;
  108.  
  109.         return '';
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ function end_img($cdata)
  114.  
  115.     /**
  116.     * @param  string 
  117.     * @return string 
  118.     * @access public
  119.     */
  120.     function end_img($cdata{
  121.         $src $this->_truePath($this->_imgAttributes['src']);
  122.  
  123.         list($w$h$t$whsgetimagesize($src);
  124.  
  125.         $this->_imgAttributes['height'$h;
  126.         $this->_imgAttributes['width']  $w;
  127.  
  128.         return sprintf(
  129.           '<img %s />',
  130.           XML_Util::attributesToString($this->_imgAttributes)
  131.         );
  132.     }
  133.  
  134.     // }}}
  135.     // {{{ start_gtext($attributes)
  136.  
  137.     /**
  138.     * @param  array 
  139.     * @return string 
  140.     * @access public
  141.     */
  142.     function start_gtext($attributes{
  143.         foreach ($this->_gtextDefaultAttributes as $k => $v{
  144.             if (isset($attributes[$k])) {
  145.                 $attributes[$k$v;
  146.             }
  147.         }
  148.  
  149.         if (!file_exists($attributes['font'])) {
  150.             $attributes['font'PEAR_XML_TRANSFORMER_IMAGE_FONTPATH . '/' $attributes['font'];
  151.         }
  152.   
  153.         $this->_gtextAttributes $attributes;
  154.   
  155.         return '';
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ function end_gtext($cdata)
  160.  
  161.     /**
  162.     * @param  string 
  163.     * @return string 
  164.     * @access public
  165.     */
  166.     function end_gtext($cdata{
  167.     if(!is_file($this->_gtextAttributes['font']))
  168.           return '<span>font \"' .
  169.             $this->_gtextAttributes['font'.
  170.             '" not found</span>';
  171.  
  172.         switch ($this->_gtextAttributes['split']{
  173.             case 'word'{
  174.                 $text preg_split('/\s+/'$cdata);
  175.  
  176.                 foreach ($text as $index => $word{
  177.                     if ($index{
  178.                         $text[$index= " $word";
  179.                     }
  180.                 }
  181.             }
  182.             break;
  183.  
  184.             case 'char'{
  185.                 $text preg_split('//'$cdata);
  186.  
  187.                 foreach ($text as $index => $word{
  188.                     if ($word == ' ' || $word == ''{
  189.                         $text[$indexchr(160);
  190.                     }
  191.                 }
  192.             }
  193.             break;
  194.  
  195.             default: {
  196.                 $text = array(0 => $cdata);
  197.             }
  198.         }
  199.  
  200.         $r '';
  201.  
  202.         foreach ($text as $index => $word{
  203.             $baseline $this->_baseline(
  204.               $this->_gtextAttributes['font'],
  205.               $this->_gtextAttributes['fontsize']
  206.             );
  207.  
  208.             $src $this->_createImage($word,$baseline);
  209.             $alt $this->_createAlt($word);
  210.  
  211.             $r .= sprintf(
  212.               '<%simg src="%s" alt="%s" />',
  213.               ($this->_prefix[0!= '&MAIN'$this->_prefix[0':' '',
  214.               $src,
  215.               addslashes($alt)
  216.             );
  217.         }
  218.  
  219.         return "<span>$r</span>";
  220.     }
  221.  
  222.     // }}}
  223.     // {{{ function start_gtextdefault($attributes)
  224.  
  225.     /**
  226.     * @param  array 
  227.     * @return string 
  228.     * @access public
  229.     */
  230.     function start_gtextdefault($attributes{
  231.         $this->_gtextDefaultAttributes $attributes;
  232.  
  233.         return '';
  234.     }
  235.  
  236.     // }}}
  237.     // {{{ function end_gtextdefault()
  238.  
  239.     /**
  240.     * @param  string 
  241.     * @return string 
  242.     * @access public
  243.     */
  244.     function end_gtextdefault({
  245.         return '';
  246.     }
  247.  
  248.     // }}}
  249.     // {{{ function _baseline($font, $size)
  250.  
  251.     /**
  252.     * @param  string 
  253.     * @param  integer 
  254.     * @return ImageTTFBBox 
  255.     * @access private
  256.     */
  257.     function _baseline($font$size{
  258.         $r = ImageTTFBBox(
  259.           $size,
  260.           0,
  261.           $font,
  262.           
  263.         );
  264.  
  265.         return $r[1];
  266.     }
  267.  
  268.     // }}}
  269.     // {{{ function _colorString($color)
  270.  
  271.     /**
  272.     * @param  integer 
  273.     * @return array 
  274.     * @access private
  275.     */
  276.     function _colorString($color{
  277.         if (substr($color01== '#'{
  278.             $color substr($color1);
  279.         }
  280.  
  281.         $r hexdec(substr($color02));
  282.         $g hexdec(substr($color22));
  283.         $b hexdec(substr($color42));
  284.  
  285.         return array($r$g$b);
  286.     }
  287.  
  288.     // }}}
  289.     // {{{ function _createAlt($word)
  290.  
  291.     /**
  292.     * @param  string 
  293.     * @return string 
  294.     * @access private
  295.     */
  296.     function _createAlt($word{
  297.         if (isset($this->_gtextAttributes['alt'])) {
  298.             return $this->_gtextAttributes['alt'];
  299.         }
  300.  
  301.         return strip_tags($word);
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ function _createImage($word, $baseline)
  306.  
  307.     /**
  308.     * @param  string 
  309.     * @param  integer 
  310.     * @return string 
  311.     * @access private
  312.     */
  313.     function _createImage($word$baseline{
  314.         $font         = isset($this->_gtextAttributes['font'])         $this->_gtextAttributes['font']         'arial.ttf';
  315.         $fh           = isset($this->_gtextAttributes['fontsize'])     $this->_gtextAttributes['fontsize']     : 12;
  316.         $bgcolor      = isset($this->_gtextAttributes['bgcolor'])      $this->_gtextAttributes['bgcolor']      '#ffffff';
  317.         $fgcolor      = isset($this->_gtextAttributes['fgcolor'])      $this->_gtextAttributes['fgcolor']      '#ffffff';
  318.  
  319.         $antialias    = isset($this->_gtextAttributes['antialias'])    $this->_gtextAttributes['antialias']    'yes';
  320.         $transparency = isset($this->_gtextAttributes['transparency']$this->_gtextAttributes['transparency''yes';
  321.         $cacheable    = isset($this->_gtextAttributes['cacheable'])    $this->_gtextAttributes['cacheable']    'yes';
  322.  
  323.         $spacing      = isset($this->_gtextAttributes['spacing'])      $this->_gtextAttributes['spacing']      : 2;
  324.         $border       = isset($this->_gtextAttributes['border'])       $this->_gtextAttributes['border']       : 0;
  325.         $bordercolor  = isset($this->_gtextAttributes['bordercolor'])  $this->_gtextAttributes['bordercolor']  '#ff0000';
  326.  
  327.         /* The cache name is derived from all attributes and cdata.
  328.          * This is very conserative and may create to many cachefiles,
  329.          * but better to err on the safe side.
  330.          */
  331.         $cachefile md5(XML_Util::attributesToString($this->_gtextAttributes':' $word'.png';
  332.         $cacheDir  $_SERVER['DOCUMENT_ROOT']
  333.                    . PEAR_XML_TRANSFORMER_IMAGE_cacheDir;
  334.         $cacheName = "$cacheDir/$cachefile";
  335.         $cacheURL  PEAR_XML_TRANSFORMER_IMAGE_cacheDir. "/$cachefile";
  336.  
  337.         if (!is_dir($cacheDir)) {
  338.             mkdir($cacheDir01777);
  339.         }
  340.  
  341.         /* Don't do the same work twice. */
  342.         if (file_exists($cacheName&& $cacheable != 'no'{
  343.             return $cacheURL;
  344.         }
  345.  
  346.         $r = ImageTTFBBox(
  347.           $fh,
  348.           0,
  349.           $font,
  350.           $word
  351.         );
  352.  
  353.         $w max(1/10*$fhabs($r[2$r[0]));
  354.         $h max(1abs($r[7$r[1]));
  355.         $x $r[0];
  356.         $y $baseline;
  357.  
  358.         $www $w  + 2*($spacing+$border);
  359.         $hhh $fh + 2*($spacing+$border);
  360.  
  361.         $im = ImageCreate($www$hhh);
  362.  
  363.         list($r$g$b$this->_colorString($bgcolor);
  364.         $bg = ImageColorAllocate($im$r$g$b);
  365.  
  366.         if ($transparency != 'no'{
  367.             ImageColorTransparent($im$bg);
  368.         }
  369.  
  370.         list($r$g$b$this->_colorString($fgcolor);
  371.         $fg = ImageColorAllocate($im$r$g$b);
  372.  
  373.         if ($antialias == 'no'{
  374.             $fg = -$fg;
  375.         }
  376.  
  377.         list($r$g$b$this->_colorString($bordercolor);
  378.         $bo = ImageColorAllocate($im$r$g$b);
  379.  
  380.         ImageFilledRectangle($im00$www$hhh$bg);
  381.  
  382.         if ($border > 0{
  383.             for ($i=$border$i>=0; $i--{
  384.                 $x1 $y1 $i;
  385.                 $x2 $www-$i-1;
  386.                 $y2 $hhh-$i-1;
  387.  
  388.                 ImageRectangle($im$x1$y1$x2$y2$bo);
  389.             }
  390.         }
  391.  
  392.         ImageTTFText(
  393.           $im,
  394.           $fh,
  395.           0,
  396.           -$x+$spacing+$border,
  397.           $hhh-(2+$y+$spacing+$border),
  398.           $fg,
  399.           $font,
  400.           $word
  401.         );
  402.  
  403.         ImagePNG($im$cacheName);
  404.         ImageDestroy($im);
  405.  
  406.         return $cacheURL;
  407.     }
  408.  
  409.     // }}}
  410.     // {{{ function _truePath($path)
  411.  
  412.     /**
  413.     * @param  string 
  414.     * @return string 
  415.     * @access private
  416.     */
  417.     function _truePath($path{
  418.         if (php_sapi_name(== 'apache'{
  419.             $uri = apache_lookup_uri($path);
  420.  
  421.             return $uri->filename;
  422.         else {
  423.             return $_SERVER['DOCUMENT_ROOT''/' $path;
  424.         }
  425.     }
  426.  
  427.     // }}}
  428. }
  429.  
  430. /*
  431.  * vim600:  et sw=2 ts=2 fdm=marker
  432.  * vim<600: et sw=2 ts=2
  433.  */
  434. ?>

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