Редакция 2 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 3 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | 8 | ||
9 | /**
|
9 | /**
|
10 | * Smarty {html_image} function plugin
|
10 | * Smarty {html_image} function plugin
|
11 | *
|
11 | *
|
12 | * Type: function<br>
|
12 | * Type: function<br>
|
13 | * Name: html_image<br>
|
13 | * Name: html_image<br>
|
14 | * Date: Feb 24, 2003<br>
|
14 | * Date: Feb 24, 2003<br>
|
15 | * Purpose: format HTML tags for the image<br>
|
15 | * Purpose: format HTML tags for the image<br>
|
16 | * Input:<br>
|
16 | * Input:<br>
|
17 | * - file = file (and path) of image (required)
|
17 | * - file = file (and path) of image (required)
|
18 | * - height = image height (optional, default actual height)
|
18 | * - height = image height (optional, default actual height)
|
19 | * - width = image width (optional, default actual width)
|
19 | * - width = image width (optional, default actual width)
|
20 | * - basedir = base directory for absolute paths, default
|
20 | * - basedir = base directory for absolute paths, default
|
21 | * is environment variable DOCUMENT_ROOT
|
21 | * is environment variable DOCUMENT_ROOT
|
22 | * - path_prefix = prefix for path output (optional, default empty)
|
22 | * - path_prefix = prefix for path output (optional, default empty)
|
23 | *
|
23 | *
|
24 | * Examples: {html_image file="/images/masthead.gif"}
|
24 | * Examples: {html_image file="/images/masthead.gif"}
|
25 | * Output: <img src="/images/masthead.gif" width=400 height=23>
|
25 | * Output: <img src="/images/masthead.gif" width=400 height=23>
|
26 | * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
|
26 | * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
|
27 | * (Smarty online manual)
|
27 | * (Smarty online manual)
|
28 | * @author Monte Ohrt <monte at ohrt dot com>
|
28 | * @author Monte Ohrt <monte at ohrt dot com>
|
29 | * @author credits to Duda <duda@big.hu> - wrote first image function
|
29 | * @author credits to Duda <duda@big.hu> - wrote first image function
|
30 | * in repository, helped with lots of functionality
|
30 | * in repository, helped with lots of functionality
|
31 | * @version 1.0
|
31 | * @version 1.0
|
32 | * @param array
|
32 | * @param array
|
33 | * @param Smarty
|
33 | * @param Smarty
|
34 | * @return string
|
34 | * @return string
|
35 | * @uses smarty_function_escape_special_chars()
|
35 | * @uses smarty_function_escape_special_chars()
|
36 | */
|
36 | */
|
37 | function smarty_function_html_image($params, &$smarty) |
37 | function smarty_function_html_image($params, &$smarty) |
38 | {
|
38 | {
|
39 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
39 | require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); |
40 | 40 | ||
41 | $alt = ''; |
41 | $alt = ''; |
42 | $file = ''; |
42 | $file = ''; |
43 | $height = ''; |
43 | $height = ''; |
44 | $width = ''; |
44 | $width = ''; |
45 | $extra = ''; |
45 | $extra = ''; |
46 | $prefix = ''; |
46 | $prefix = ''; |
47 | $suffix = ''; |
47 | $suffix = ''; |
48 | $path_prefix = ''; |
48 | $path_prefix = ''; |
49 | $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS']; |
49 | $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS']; |
50 | $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : ''; |
50 | $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : ''; |
51 | foreach($params as $_key => $_val) { |
51 | foreach($params as $_key => $_val) { |
52 | switch($_key) { |
52 | switch($_key) { |
53 | case 'file': |
53 | case 'file': |
54 | case 'height': |
54 | case 'height': |
55 | case 'width': |
55 | case 'width': |
56 | case 'dpi': |
56 | case 'dpi': |
57 | case 'path_prefix': |
57 | case 'path_prefix': |
58 | case 'basedir': |
58 | case 'basedir': |
59 | $$_key = $_val; |
59 | $$_key = $_val; |
60 | break; |
60 | break; |
61 | 61 | ||
62 | case 'alt': |
62 | case 'alt': |
63 | if(!is_array($_val)) { |
63 | if(!is_array($_val)) { |
64 | $$_key = smarty_function_escape_special_chars($_val); |
64 | $$_key = smarty_function_escape_special_chars($_val); |
65 | } else { |
65 | } else { |
66 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
66 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
67 | }
|
67 | }
|
68 | break; |
68 | break; |
69 | 69 | ||
70 | case 'link': |
70 | case 'link': |
71 | case 'href': |
71 | case 'href': |
72 | $prefix = '<a href="' . $_val . '">'; |
72 | $prefix = '<a href="' . $_val . '">'; |
73 | $suffix = '</a>'; |
73 | $suffix = '</a>'; |
74 | break; |
74 | break; |
75 | 75 | ||
76 | default: |
76 | default: |
77 | if(!is_array($_val)) { |
77 | if(!is_array($_val)) { |
78 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
78 | $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; |
79 | } else { |
79 | } else { |
80 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
80 | $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE); |
81 | }
|
81 | }
|
82 | break; |
82 | break; |
83 | }
|
83 | }
|
84 | }
|
84 | }
|
85 | 85 | ||
86 | if (empty($file)) { |
86 | if (empty($file)) { |
87 | $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE); |
87 | $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE); |
88 | return; |
88 | return; |
89 | }
|
89 | }
|
90 | 90 | ||
91 | if (substr($file,0,1) == '/') { |
91 | if (substr($file,0,1) == '/') { |
92 | $_image_path = $basedir . $file; |
92 | $_image_path = $basedir . $file; |
93 | } else { |
93 | } else { |
94 | $_image_path = $file; |
94 | $_image_path = $file; |
95 | }
|
95 | }
|
96 | 96 | ||
97 | if(!isset($params['width']) || !isset($params['height'])) { |
97 | if(!isset($params['width']) || !isset($params['height'])) { |
98 | if(!$_image_data = @getimagesize($_image_path)) { |
98 | if(!$_image_data = @getimagesize($_image_path)) { |
99 | if(!file_exists($_image_path)) { |
99 | if(!file_exists($_image_path)) { |
100 | $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE); |
100 | $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE); |
101 | return; |
101 | return; |
102 | } else if(!is_readable($_image_path)) { |
102 | } else if(!is_readable($_image_path)) { |
103 | $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE); |
103 | $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE); |
104 | return; |
104 | return; |
105 | } else { |
105 | } else { |
106 | $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE); |
106 | $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE); |
107 | return; |
107 | return; |
108 | }
|
108 | }
|
109 | }
|
109 | }
|
110 | if ($smarty->security && |
110 | if ($smarty->security && |
111 | ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) && |
111 | ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) && |
112 | (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) && |
112 | (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) && |
113 | (!smarty_core_is_secure($_params, $smarty)) ) { |
113 | (!smarty_core_is_secure($_params, $smarty)) ) { |
114 | $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE); |
114 | $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE); |
115 | }
|
115 | }
|
116 | 116 | ||
117 | if(!isset($params['width'])) { |
117 | if(!isset($params['width'])) { |
118 | $width = $_image_data[0]; |
118 | $width = $_image_data[0]; |
119 | }
|
119 | }
|
120 | if(!isset($params['height'])) { |
120 | if(!isset($params['height'])) { |
121 | $height = $_image_data[1]; |
121 | $height = $_image_data[1]; |
122 | }
|
122 | }
|
123 | 123 | ||
124 | }
|
124 | }
|
125 | 125 | ||
126 | if(isset($params['dpi'])) { |
126 | if(isset($params['dpi'])) { |
127 | if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) { |
127 | if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) { |
128 | $dpi_default = 72; |
128 | $dpi_default = 72; |
129 | } else { |
129 | } else { |
130 | $dpi_default = 96; |
130 | $dpi_default = 96; |
131 | }
|
131 | }
|
132 | $_resize = $dpi_default/$params['dpi']; |
132 | $_resize = $dpi_default/$params['dpi']; |
133 | $width = round($width * $_resize); |
133 | $width = round($width * $_resize); |
134 | $height = round($height * $_resize); |
134 | $height = round($height * $_resize); |
135 | }
|
135 | }
|
136 | 136 | ||
137 | return $prefix . '<img src="'.$path_prefix.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix; |
137 | return $prefix . '<img src="'.$path_prefix.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix; |
138 | }
|
138 | }
|
139 | 139 | ||
140 | /* vim: set expandtab: */
|
140 | /* vim: set expandtab: */
|
141 | 141 | ||
142 | ?>
|
142 | ?>
|
143 | 143 |