home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / function.html_image.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.9 KB  |  137 lines

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8.  
  9. /**
  10.  * Smarty {html_image} function plugin
  11.  *
  12.  * Type:     function<br>
  13.  * Name:     html_image<br>
  14.  * Date:     Feb 24, 2003<br>
  15.  * Purpose:  format HTML tags for the image<br>
  16.  * Input:<br>
  17.  *         - file = file (and path) of image (required)
  18.  *         - border = border width (optional, default 0)
  19.  *         - height = image height (optional, default actual height)
  20.  *         - image =image width (optional, default actual width)
  21.  *         - basedir = base directory for absolute paths, default
  22.  *                     is environment variable DOCUMENT_ROOT
  23.  * 
  24.  * Examples: {image file="images/masthead.gif"}
  25.  * Output:   <img src="images/masthead.gif" border=0 width=400 height=23>
  26.  * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
  27.  *      (Smarty online manual)
  28.  * @author   Monte Ohrt <monte@ispi.net>
  29.  * @author credits to Duda <duda@big.hu> - wrote first image function
  30.  *           in repository, helped with lots of functionality
  31.  * @version  1.0
  32.  * @param array
  33.  * @param Smarty
  34.  * @return string
  35.  * @uses smarty_function_escape_special_chars()
  36.  */
  37. function smarty_function_html_image($params, &$smarty)
  38. {    
  39.     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  40.  
  41.     $file = '';
  42.     $border = 0;
  43.     $height = '';
  44.     $width = '';
  45.     $extra = '';
  46.     $prefix = '';
  47.     $suffix = '';
  48.     $basedir = isset($GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'])
  49.             ? $GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'] : '/';
  50.     if(strstr($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT'], 'Mac')) {
  51.         $dpi_default = 72;
  52.     } else {
  53.         $dpi_default = 96;
  54.     }
  55.     
  56.     foreach($params as $_key => $_val) {    
  57.         switch($_key) {
  58.             case 'file':
  59.                 $file = $_val;
  60.                 break;
  61.             case 'border':
  62.                 $border = $_val;
  63.                 break;
  64.             case 'height':
  65.                 $height = $_val;
  66.                 break;
  67.             case 'width':
  68.                 $width = $_val;
  69.                 break;
  70.             case 'link':
  71.                 $prefix = '<a href="' . $_val . '">';
  72.                 $suffix = '</a>';
  73.                 break;
  74.             case 'dpi':
  75.                 $dpi = $_val;
  76.                 break;
  77.             default:
  78.                 if(!is_array($_val)) {
  79.                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  80.                 } else {
  81.                     $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  82.                 }
  83.                 break;                    
  84.         }
  85.     }
  86.  
  87.     if (empty($file)) {
  88.         $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
  89.         return;
  90.     }
  91.  
  92.     if(substr($file,0,1) == DIR_SEP) {
  93.         $_image_path = $basedir . DIR_SEP . $file;
  94.     } else {
  95.         $_image_path = $file;
  96.     }
  97.     
  98.     if(!isset($params['width']) || !isset($params['height'])) {
  99.         if(!$_image_data = @getimagesize($_image_path)) {
  100.             if(!file_exists($_image_path)) {
  101.                 $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);        
  102.                 return;
  103.             } else if(!is_readable($_image_path)) {
  104.                 $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);        
  105.                 return;
  106.             } else {
  107.                 $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
  108.                 return;
  109.             }
  110.         }
  111.         if(!$smarty->security && !$smarty->_is_secure('file', $_image_path)) {
  112.             $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);        
  113.             return;
  114.         }    
  115.         
  116.         if(!isset($params['width'])) {
  117.             $width = $_image_data[0];
  118.         }
  119.         if(!isset($params['height'])) {
  120.             $height = $_image_data[1];
  121.         }
  122.         
  123.     }
  124.  
  125.     if(isset($params['dpi'])) {
  126.         $_resize = $dpi_default/$params['dpi'];
  127.         $width = round($width * $_resize);
  128.         $height = round($height * $_resize);
  129.     }
  130.             
  131.     return $prefix . '<img src="'.$file.'" border="'.$border.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix;
  132. }
  133.  
  134. /* vim: set expandtab: */
  135.  
  136. ?>
  137.