10 * Smarty {html_image} function plugin
13 * Name: html_image<br>
14 * Date: Feb 24, 2003<br>
15 * Purpose: format HTML tags for the image<br>
17 * - file = file (and path) of image (required)
18 * - height = image height (optional, default actual height)
19 * - width = image width (optional, default actual width)
20 * - basedir = base directory for absolute paths, default
21 * is environment variable DOCUMENT_ROOT
23 * Examples: {html_image file="images/masthead.gif"}
24 * Output: <img src="images/masthead.gif" width=400 height=23>
25 * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
26 * (Smarty online manual)
27 * @author Monte Ohrt <monte at ohrt dot com>
28 * @author credits to Duda <duda@big.hu> - wrote first image function
29 * in repository, helped with lots of functionality
34 * @uses smarty_function_escape_special_chars()
36 function smarty_function_html_image($params, &$smarty)
38 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
47 $server_vars = ($smarty->request_use_auto_globals
) ?
$_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
48 $basedir = isset($server_vars['DOCUMENT_ROOT']) ?
$server_vars['DOCUMENT_ROOT'] : '';
49 foreach($params as $_key => $_val) {
60 if(!is_array($_val)) {
61 $
$_key = smarty_function_escape_special_chars($_val);
63 $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE
);
69 $prefix = '<a href="' . $_val . '">';
74 if(!is_array($_val)) {
75 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
77 $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE
);
84 $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE
);
88 if (substr($file,0,1) == '/') {
89 $_image_path = $basedir . $file;
94 if(!isset($params['width']) ||
!isset($params['height'])) {
95 if ($smarty->security
&&
96 ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
97 (require_once(SMARTY_CORE_DIR
. 'core.is_secure.php')) &&
98 (!smarty_core_is_secure($_params, $smarty)) ) {
99 $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE
);
101 } elseif (!$_image_data = @getimagesize
($_image_path)) {
102 if(!file_exists($_image_path)) {
103 $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE
);
105 } else if(!is_readable($_image_path)) {
106 $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE
);
109 $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE
);
114 if(!isset($params['width'])) {
115 $width = $_image_data[0];
117 if(!isset($params['height'])) {
118 $height = $_image_data[1];
123 if(isset($params['dpi'])) {
124 if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
129 $_resize = $dpi_default/$params['dpi'];
130 $width = round($width * $_resize);
131 $height = round($height * $_resize);
134 return $prefix . '<img src="'.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix;
137 /* vim: set expandtab: */