includes/wikiengine/Render/Xhtml/Image.php
changeset 1027 98c052fc3337
parent 1026 f0431eb8161e
child 1028 dde4416dea00
equal deleted inserted replaced
1026:f0431eb8161e 1027:98c052fc3337
     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.16 2006/02/10 23:07:03 toggg 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         // we make up an align="center" value for the <img> tag.
       
    86         if (isset($options['attr']['align']) &&
       
    87             $options['attr']['align'] == 'center') {
       
    88 
       
    89             // unset so it won't show up as an attribute
       
    90             unset($options['attr']['align']);
       
    91 
       
    92             // make sure we have a style attribute
       
    93             if (! isset($options['attr']['style'])) {
       
    94                 // no style, set up a blank one
       
    95                 $options['attr']['style'] = '';
       
    96             } else {
       
    97                 // style exists, add a space
       
    98                 $options['attr']['style'] .= ' ';
       
    99             }
       
   100 
       
   101             // add a "center" style to the existing style.
       
   102             $options['attr']['style'] .=
       
   103                 'display: block; margin-left: auto; margin-right: auto;';
       
   104         }
       
   105 
       
   106         // stephane@metacites.net -- 25/07/2004
       
   107         // try to guess width and height
       
   108         if (! isset($options['attr']['width']) &&
       
   109             ! isset($options['attr']['height'])) {
       
   110 
       
   111             // does the source refer to a local file or a URL?
       
   112             if (strpos($src,'://')) {
       
   113                 // is a URL link
       
   114                 $imageFile = $src;
       
   115             } elseif ($src[0] == '.') {
       
   116             	// reg at dav-muz dot net -- 2005-03-07
       
   117 				// is a local file on relative path.
       
   118 				$imageFile = $src; # ...don't do anything because it's perfect!
       
   119 			} else {
       
   120                 // is a local file on absolute path.
       
   121                 $imageFile = $_SERVER['DOCUMENT_ROOT'] . $src;
       
   122             }
       
   123 
       
   124             // attempt to get the image size
       
   125             $imageSize = @getimagesize($imageFile);
       
   126 
       
   127             if (is_array($imageSize)) {
       
   128                 $options['attr']['width'] = $imageSize[0];
       
   129                 $options['attr']['height'] = $imageSize[1];
       
   130             }
       
   131 
       
   132         }
       
   133 
       
   134         // start the HTML output
       
   135         $output = '<img src="' . $this->textEncode($src) . '"';
       
   136 
       
   137         // get the CSS class but don't add it yet
       
   138         $css = $this->formatConf(' class="%s"', 'css');
       
   139 
       
   140         // add the attributes to the output, and be sure to
       
   141         // track whether or not we find an "alt" attribute
       
   142         $alt = false;
       
   143         foreach ($options['attr'] as $key => $val) {
       
   144 
       
   145             // track the 'alt' attribute
       
   146             if (strtolower($key) == 'alt') {
       
   147                 $alt = true;
       
   148             }
       
   149 
       
   150             // the 'class' attribute overrides the CSS class conf
       
   151             if (strtolower($key) == 'class') {
       
   152                 $css = null;
       
   153             }
       
   154 
       
   155             $key = $this->textEncode($key);
       
   156             $val = $this->textEncode($val);
       
   157             $output .= " $key=\"$val\"";
       
   158         }
       
   159 
       
   160         // always add an "alt" attribute per Stephane Solliec
       
   161         if (! $alt) {
       
   162             $alt = $this->textEncode(basename($options['src']));
       
   163             $output .= " alt=\"$alt\"";
       
   164         }
       
   165 
       
   166         // end the image tag with the automatic CSS class (if any)
       
   167         $output .= "$css />";
       
   168 
       
   169         // was the image clickable?
       
   170         if ($href) {
       
   171             // yes, add the href and return
       
   172             $href = $this->textEncode($href);
       
   173             $css = $this->formatConf(' class="%s"', 'css_link');
       
   174             $output = "<a$css href=\"$href\">$output</a>";
       
   175         }
       
   176 
       
   177         return $output;
       
   178     }
       
   179 }
       
   180 ?>