6 * @subpackage PluginsFunction
10 * Smarty {html_image} function plugin
12 * Name: html_image<br>
13 * Date: Feb 24, 2003<br>
14 * Purpose: format HTML tags for the image<br>
15 * Examples: {html_image file="/images/masthead.gif"}<br>
16 * Output: <img src="/images/masthead.gif" width=400 height=23><br>
19 * - file - (required) - file (and path) of image
20 * - height - (optional) - image height (default actual height)
21 * - width - (optional) - image width (default actual width)
22 * - basedir - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
23 * - path_prefix - prefix for path output (optional, default empty)
26 * @link http://www.smarty.net/manual/en/language.function.html.image.php {html_image}
27 * (Smarty online manual)
28 * @author Monte Ohrt <monte at ohrt dot com>
29 * @author credits to Duda <duda@big.hu>
32 * @param array $params parameters
33 * @param Smarty_Internal_Template $template template object
35 * @throws SmartyException
37 * @uses smarty_function_escape_special_chars()
39 function smarty_function_html_image($params, $template)
41 if (!is_callable('smarty_function_escape_special_chars')) {
42 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
53 $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : '';
54 foreach ($params as $_key => $_val) {
66 if (!is_array($_val)) {
67 $$_key = smarty_function_escape_special_chars($_val);
69 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
75 $prefix = '<a href="' . $_val . '">';
80 if (!is_array($_val)) {
81 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
83 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
90 trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
95 if ($file[ 0 ] == '/') {
96 $_image_path = $basedir . $file;
101 // strip file protocol
102 if (stripos($params[ 'file' ], 'file://') === 0) {
103 $params[ 'file' ] = substr($params[ 'file' ], 7);
106 $protocol = strpos($params[ 'file' ], '://');
107 if ($protocol !== false) {
108 $protocol = strtolower(substr($params[ 'file' ], 0, $protocol));
111 if (isset($template->smarty->security_policy)) {
113 // remote resource (or php stream, …)
114 if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) {
119 if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) {
125 if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) {
126 // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
127 if (!$_image_data = @getimagesize($_image_path)) {
128 if (!file_exists($_image_path)) {
129 trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
132 } elseif (!is_readable($_image_path)) {
133 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
137 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
143 if (!isset($params[ 'width' ])) {
144 $width = $_image_data[ 0 ];
146 if (!isset($params[ 'height' ])) {
147 $height = $_image_data[ 1 ];
151 if (isset($params[ 'dpi' ])) {
152 if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) {
153 // FIXME: (rodneyrehm) wrong dpi assumption
154 // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
159 $_resize = $dpi_default / $params[ 'dpi' ];
160 $width = round($width * $_resize);
161 $height = round($height * $_resize);
164 return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' .
165 $height . '"' . $extra . ' />' . $suffix;