1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
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 * Examples: {html_image file="/images/masthead.gif"}<br>
17 * Output: <img src="/images/masthead.gif" width=400 height=23><br>
18 * Params:
19 * <pre>
20 * - file - (required) - file (and path) of image
21 * - height - (optional) - image height (default actual height)
22 * - width - (optional) - image width (default actual width)
23 * - basedir - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
24 * - path_prefix - prefix for path output (optional, default empty)
25 * </pre>
26 *
27 * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
28 * (Smarty online manual)
29 * @author Monte Ohrt <monte at ohrt dot com>
30 * @author credits to Duda <duda@big.hu>
31 * @version 1.0
32 * @param array $params parameters
33 * @param Smarty_Internal_Template $template template object
34 * @return string
35 * @uses smarty_function_escape_special_chars()
36 */
37 function smarty_function_html_image($params, $template)
38 {
39 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
41 $alt = '';
42 $file = '';
43 $height = '';
44 $width = '';
45 $extra = '';
46 $prefix = '';
47 $suffix = '';
48 $path_prefix = '';
49 $server_vars = $_SERVER;
50 $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
51 foreach($params as $_key => $_val) {
52 switch ($_key) {
53 case 'file':
54 case 'height':
55 case 'width':
56 case 'dpi':
57 case 'path_prefix':
58 case 'basedir':
59 $$_key = $_val;
60 break;
62 case 'alt':
63 if (!is_array($_val)) {
64 $$_key = smarty_function_escape_special_chars($_val);
65 } else {
66 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
67 }
68 break;
70 case 'link':
71 case 'href':
72 $prefix = '<a href="' . $_val . '">';
73 $suffix = '</a>';
74 break;
76 default:
77 if (!is_array($_val)) {
78 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
79 } else {
80 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
81 }
82 break;
83 }
84 }
86 if (empty($file)) {
87 trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
88 return;
89 }
91 if (substr($file, 0, 1) == '/') {
92 $_image_path = $basedir . $file;
93 } else {
94 $_image_path = $file;
95 }
97 if (!isset($params['width']) || !isset($params['height'])) {
98 if (!$_image_data = @getimagesize($_image_path)) {
99 if (!file_exists($_image_path)) {
100 trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
101 return;
102 } else if (!is_readable($_image_path)) {
103 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
104 return;
105 } else {
106 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
107 return;
108 }
109 }
110 if (isset($template->smarty->security_policy)) {
111 if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
112 return;
113 }
114 }
116 if (!isset($params['width'])) {
117 $width = $_image_data[0];
118 }
119 if (!isset($params['height'])) {
120 $height = $_image_data[1];
121 }
122 }
124 if (isset($params['dpi'])) {
125 if (strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
126 $dpi_default = 72;
127 } else {
128 $dpi_default = 96;
129 }
130 $_resize = $dpi_default / $params['dpi'];
131 $width = round($width * $_resize);
132 $height = round($height * $_resize);
133 }
135 return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
136 }
138 ?>