51ff3226 |
1 | <?php |
2 | /** |
3 | * Smarty plugin |
4 | * @package Smarty |
5 | * @subpackage plugins |
6 | */ |
7 | |
8 | |
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 | * Input:<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 |
22 | * |
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 |
30 | * @version 1.0 |
31 | * @param array |
32 | * @param Smarty |
33 | * @return string |
34 | * @uses smarty_function_escape_special_chars() |
35 | */ |
36 | function smarty_function_html_image($params, &$smarty) |
37 | { |
38 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
39 | |
40 | $alt = ''; |
41 | $file = ''; |
42 | $height = ''; |
43 | $width = ''; |
44 | $extra = ''; |
45 | $prefix = ''; |
46 | $suffix = ''; |
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) { |
50 | switch($_key) { |
51 | case 'file': |
52 | case 'height': |
53 | case 'width': |
54 | case 'dpi': |
55 | case 'basedir': |
56 | $$_key = $_val; |
57 | break; |
58 | |
59 | case 'alt': |
60 | if(!is_array($_val)) { |
61 | $$_key = smarty_function_escape_special_chars($_val); |
62 | } else { |
63 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
64 | } |
65 | break; |
66 | |
67 | case 'link': |
68 | case 'href': |
69 | $prefix = '<a href="' . $_val . '">'; |
70 | $suffix = '</a>'; |
71 | break; |
72 | |
73 | default: |
74 | if(!is_array($_val)) { |
75 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
76 | } else { |
77 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
78 | } |
79 | break; |
80 | } |
81 | } |
82 | |
83 | if (empty($file)) { |
84 | $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE); |
85 | return; |
86 | } |
87 | |
88 | if (substr($file,0,1) == '/') { |
89 | $_image_path = $basedir . $file; |
90 | } else { |
91 | $_image_path = $file; |
92 | } |
93 | |
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); |
100 | |
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); |
104 | return; |
105 | } else if(!is_readable($_image_path)) { |
106 | $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE); |
107 | return; |
108 | } else { |
109 | $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE); |
110 | return; |
111 | } |
112 | } |
113 | |
114 | if(!isset($params['width'])) { |
115 | $width = $_image_data[0]; |
116 | } |
117 | if(!isset($params['height'])) { |
118 | $height = $_image_data[1]; |
119 | } |
120 | |
121 | } |
122 | |
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 | } |
133 | |
134 | return $prefix . '<img src="'.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix; |
135 | } |
136 | |
137 | /* vim: set expandtab: */ |
138 | |
139 | ?> |