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