home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Render / Xhtml / Image.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.9 KB  |  185 lines

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
  3. /**
  4.  * Image rule end renderer for Xhtml
  5.  *
  6.  * PHP versions 4 and 5
  7.  *
  8.  * @category   Text
  9.  * @package    Text_Wiki
  10.  * @author     Paul M. Jones <pmjones@php.net>
  11.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  12.  * @version    CVS: $Id: Image.php,v 1.17 2007/03/15 15:04:50 justinpatrin Exp $
  13.  * @link       http://pear.php.net/package/Text_Wiki
  14.  */
  15.  
  16. /**
  17.  * This class inserts an image in XHTML.
  18.  *
  19.  * @category   Text
  20.  * @package    Text_Wiki
  21.  * @author     Paul M. Jones <pmjones@php.net>
  22.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  23.  * @version    Release: @package_version@
  24.  * @link       http://pear.php.net/package/Text_Wiki
  25.  */
  26. class Text_Wiki_Render_Xhtml_Image extends Text_Wiki_Render {
  27.  
  28.     var $conf = array(
  29.         'base' => '/',
  30.         'url_base' => null,
  31.         'css'  => null,
  32.         'css_link' => null
  33.     );
  34.  
  35.  
  36.     /**
  37.     *
  38.     * Renders a token into text matching the requested format.
  39.     *
  40.     * @access public
  41.     *
  42.     * @param array $options The "options" portion of the token (second
  43.     * element).
  44.     *
  45.     * @return string The text rendered from the token options.
  46.     *
  47.     */
  48.  
  49.     function token($options)
  50.     {
  51.         // note the image source
  52.         $src = $options['src'];
  53.  
  54.         // is the source a local file or URL?
  55.         if (strpos($src, '://') === false) {
  56.             // the source refers to a local file.
  57.             // add the URL base to it.
  58.             $src = $this->getConf('base', '/') . $src;
  59.         }
  60.  
  61.         // stephane@metacites.net
  62.         // is the image clickable?
  63.         if (isset($options['attr']['link'])) {
  64.             // yes, the image is clickable.
  65.             // are we linked to a URL or a wiki page?
  66.             if (strpos($options['attr']['link'], '://')) {
  67.                 // it's a URL, prefix the URL base
  68.                 $href = $this->getConf('url_base') . $options['attr']['link'];
  69.             } else {
  70.                 // it's a WikiPage; assume it exists.
  71.                 /** @todo This needs to honor sprintf wikilinks (pmjones) */
  72.                 /** @todo This needs to honor interwiki (pmjones) */
  73.                 /** @todo This needs to honor freelinks (pmjones) */
  74.                 $href = $this->wiki->getRenderConf('xhtml', 'wikilink', 'view_url') .
  75.                     $options['attr']['link'];
  76.             }
  77.         } else {
  78.             // image is not clickable.
  79.             $href = null;
  80.         }
  81.         // unset so it won't show up as an attribute
  82.         unset($options['attr']['link']);
  83.  
  84.         // stephane@metacites.net -- 25/07/2004
  85.         // use CSS for all alignment
  86.         if (isset($options['attr']['align'])) {
  87.             // make sure we have a style attribute
  88.             if (!isset($options['attr']['style'])) {
  89.                 // no style, set up a blank one
  90.                 $options['attr']['style'] = '';
  91.             } else {
  92.                 // style exists, add a space
  93.                 $options['attr']['style'] .= ' ';
  94.             }
  95.  
  96.             if ($options['attr']['align'] == 'center') {
  97.                 // add a "center" style to the existing style.
  98.                 $options['attr']['style'] .=
  99.                     'display: block; margin-left: auto; margin-right: auto;';
  100.             } else {
  101.                 // add a float style to the existing style
  102.                 $options['attr']['style'] .=
  103.                     'float: '.$options['attr']['align'];
  104.             }
  105.             
  106.             // unset so it won't show up as an attribute
  107.             unset($options['attr']['align']);
  108.         }
  109.  
  110.         // stephane@metacites.net -- 25/07/2004
  111.         // try to guess width and height
  112.         if (! isset($options['attr']['width']) &&
  113.             ! isset($options['attr']['height'])) {
  114.  
  115.             // does the source refer to a local file or a URL?
  116.             if (strpos($src,'://')) {
  117.                 // is a URL link
  118.                 $imageFile = $src;
  119.             } elseif ($src[0] == '.') {
  120.                 // reg at dav-muz dot net -- 2005-03-07
  121.                 // is a local file on relative path.
  122.                 $imageFile = $src; # ...don't do anything because it's perfect!
  123.             } else {
  124.                 // is a local file on absolute path.
  125.                 $imageFile = $_SERVER['DOCUMENT_ROOT'] . $src;
  126.             }
  127.  
  128.             // attempt to get the image size
  129.             $imageSize = @getimagesize($imageFile);
  130.  
  131.             if (is_array($imageSize)) {
  132.                 $options['attr']['width'] = $imageSize[0];
  133.                 $options['attr']['height'] = $imageSize[1];
  134.             }
  135.  
  136.         }
  137.  
  138.         // start the HTML output
  139.         $output = '<img src="' . $this->textEncode($src) . '"';
  140.  
  141.         // get the CSS class but don't add it yet
  142.         $css = $this->formatConf(' class="%s"', 'css');
  143.  
  144.         // add the attributes to the output, and be sure to
  145.         // track whether or not we find an "alt" attribute
  146.         $alt = false;
  147.         foreach ($options['attr'] as $key => $val) {
  148.  
  149.             // track the 'alt' attribute
  150.             if (strtolower($key) == 'alt') {
  151.                 $alt = true;
  152.             }
  153.  
  154.             // the 'class' attribute overrides the CSS class conf
  155.             if (strtolower($key) == 'class') {
  156.                 $css = null;
  157.             }
  158.  
  159.             $key = $this->textEncode($key);
  160.             $val = $this->textEncode($val);
  161.             $output .= " $key=\"$val\"";
  162.         }
  163.  
  164.         // always add an "alt" attribute per Stephane Solliec
  165.         if (! $alt) {
  166.             $alt = $this->textEncode(basename($options['src']));
  167.             $output .= " alt=\"$alt\"";
  168.         }
  169.  
  170.         // end the image tag with the automatic CSS class (if any)
  171.         $output .= "$css />";
  172.  
  173.         // was the image clickable?
  174.         if ($href) {
  175.             // yes, add the href and return
  176.             $href = $this->textEncode($href);
  177.             $css = $this->formatConf(' class="%s"', 'css_link');
  178.             $output = "<a$css href=\"$href\">$output</a>";
  179.         }
  180.  
  181.         return $output;
  182.     }
  183. }
  184. ?>
  185.