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 $smarty Smarty object
33 * @param object $template template object
34 * @return string
35 * @uses smarty_function_escape_special_chars()
36 */
37 function smarty_function_html_image($params, $smarty, $template)
38 {
39 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
40 //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
42 $alt = '';
43 $file = '';
44 $height = '';
45 $width = '';
46 $extra = '';
47 $prefix = '';
48 $suffix = '';
49 $path_prefix = '';
50 $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
51 $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
52 foreach($params as $_key => $_val) {
53 switch ($_key) {
54 case 'file':
55 case 'height':
56 case 'width':
57 case 'dpi':
58 case 'path_prefix':
59 case 'basedir':
60 $$_key = $_val;
61 break;
63 case 'alt':
64 if (!is_array($_val)) {
65 $$_key = smarty_function_escape_special_chars($_val);
66 } else {
67 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
68 }
69 break;
71 case 'link':
72 case 'href':
73 $prefix = '<a href="' . $_val . '">';
74 $suffix = '</a>';
75 break;
77 default:
78 if (!is_array($_val)) {
79 $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
80 } else {
81 throw new SmartyException ("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
82 }
83 break;
84 }
85 }
87 if (empty($file)) {
88 trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
89 return;
90 }
92 if (substr($file, 0, 1) == '/') {
93 $_image_path = $basedir . $file;
94 } else {
95 $_image_path = $file;
96 }
98 if (!isset($params['width']) || !isset($params['height'])) {
99 if (!$_image_data = @getimagesize($_image_path)) {
100 if (!file_exists($_image_path)) {
101 trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
102 return;
103 } else if (!is_readable($_image_path)) {
104 trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
105 return;
106 } else {
107 trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
108 return;
109 }
110 }
111 if ($template->security) {
112 if (!$smarty->security_handler->isTrustedResourceDir($_image_path)) {
113 return;
114 }
115 }
117 if (!isset($params['width'])) {
118 $width = $_image_data[0];
119 }
120 if (!isset($params['height'])) {
121 $height = $_image_data[1];
122 }
123 }
125 if (isset($params['dpi'])) {
126 if (strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
127 $dpi_default = 72;
128 } else {
129 $dpi_default = 96;
130 }
131 $_resize = $dpi_default / $params['dpi'];
132 $width = round($width * $_resize);
133 $height = round($height * $_resize);
134 }
136 return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
137 }
139 ?>