phpDocumentor Smarty
plugins
[ class tree: Smarty ] [ index: Smarty ] [ all elements ]

Source for file function.html_image.php

Documentation is available at function.html_image.php

  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: {html_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 <[email protected]>
  29.  * @author credits to Duda <[email protected]> - 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.     $alt '';
  42.     $file '';
  43.     $border 0;
  44.     $height '';
  45.     $width '';
  46.     $extra '';
  47.     $prefix '';
  48.     $suffix '';
  49.     $basedir = isset($GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'])
  50.         ? $GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT''';
  51.     if(strstr($GLOBALS['HTTP_SERVER_VARS']['HTTP_USER_AGENT']'Mac')) {
  52.         $dpi_default 72;
  53.     else {
  54.         $dpi_default 96;
  55.     }
  56.  
  57.     foreach($params as $_key => $_val{
  58.         switch($_key{
  59.             case 'file':
  60.             case 'border':
  61.             case 'height':
  62.             case 'width':
  63.             case 'dpi':
  64.             case 'basedir':
  65.                 $$_key $_val;
  66.                 break;
  67.  
  68.             case 'alt':
  69.                 if(!is_array($_val)) {
  70.                     $$_key smarty_function_escape_special_chars($_val);
  71.                 else {
  72.                     $smarty->trigger_error("html_imageextra attribute '$_keycannot be an array"E_USER_NOTICE);
  73.                 }
  74.                 break;
  75.  
  76.             case 'link':
  77.             case 'href':
  78.                 $prefix '<a href="' $_val '">';
  79.                 $suffix '</a>';
  80.                 break;
  81.  
  82.             default:
  83.                 if(!is_array($_val)) {
  84.                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  85.                 else {
  86.                     $smarty->trigger_error("html_imageextra attribute '$_keycannot be an array"E_USER_NOTICE);
  87.                 }
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     if (empty($file)) {
  93.         $smarty->trigger_error("html_image: missing 'file' parameter"E_USER_NOTICE);
  94.         return;
  95.     }
  96.  
  97.     if (substr($file,0,1== '/'{
  98.         $_image_path $basedir $file;
  99.     else {
  100.         $_image_path $file;
  101.     }
  102.  
  103.     if(!isset($params['width']|| !isset($params['height'])) {
  104.         if(!$_image_data @getimagesize($_image_path)) {
  105.             if(!file_exists($_image_path)) {
  106.                 $smarty->trigger_error("html_imageunable to find '$_image_path'"E_USER_NOTICE);
  107.                 return;
  108.             else if(!is_readable($_image_path)) {
  109.                 $smarty->trigger_error("html_imageunable to read '$_image_path'"E_USER_NOTICE);
  110.                 return;
  111.             else {
  112.                 $smarty->trigger_error("html_image: '$_image_pathis not a valid image file"E_USER_NOTICE);
  113.                 return;
  114.             }
  115.         }
  116.         $_params array('resource_type' => 'file''resource_name' => $_image_path);
  117.         require_once(SMARTY_DIR 'core' DIRECTORY_SEPARATOR 'core.is_secure.php');
  118.         if(!$smarty->security && !smarty_core_is_secure($_params$smarty)) {
  119.             $smarty->trigger_error("html_image: (secure) '$_image_pathnot in secure directory"E_USER_NOTICE);
  120.             return;
  121.         }
  122.  
  123.         if(!isset($params['width'])) {
  124.             $width $_image_data[0];
  125.         }
  126.         if(!isset($params['height'])) {
  127.             $height $_image_data[1];
  128.         }
  129.  
  130.     }
  131.  
  132.     if(isset($params['dpi'])) {
  133.         $_resize $dpi_default/$params['dpi'];
  134.         $width round($width $_resize);
  135.         $height round($height $_resize);
  136.     }
  137.  
  138.     return $prefix '<img src="'.$file.'" alt="'.$alt.'" border="'.$border.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' $suffix;
  139. }
  140.  
  141. /* vim: set expandtab: */
  142.  
  143. ?>

Documentation generated on Tue, 24 Oct 2006 09:22:38 -0500 by phpDocumentor 1.3.1