]> git.pld-linux.org Git - packages/Smarty.git/blob - Smarty-function.html_input_image.php
- use virtual php extension deps (to be independant of php version)
[packages/Smarty.git] / Smarty-function.html_input_image.php
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_input_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  *         - path_prefix = prefix for path output (optional, default empty)
23  *
24  * Examples: {html_input_image file="/images/masthead.gif"}
25  * Output:   <input type="image" src="/images/masthead.gif" width=400 height=23>
26  * @link http://smarty.php.net/manual/en/language.function.html.input_image.php {html_input_image}
27  *      (Smarty online manual)
28  * @author   Monte Ohrt <monte at ohrt dot com>
29  * @author credits to Duda <duda@big.hu> - wrote first image function
30  *           in repository, helped with lots of functionality
31  * @author   Elan Ruusamäe <glen@delfi.ee>
32  * @version  1.0
33  * @param array
34  * @param Smarty
35  * @return string
36  * @uses smarty_function_escape_special_chars()
37  */
38 function smarty_function_html_input_image($params, &$smarty)
39 {
40     require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
41     
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;
62
63             case 'alt':
64                 if(!is_array($_val)) {
65                     $$_key = smarty_function_escape_special_chars($_val);
66                 } else {
67                     $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
68                 }
69                 break;
70
71                 // FIXME: link/href makes no sense?
72             case 'link':
73             case 'href':
74                 $prefix = '<a href="' . $_val . '">';
75                 $suffix = '</a>';
76                 break;
77
78             default:
79                 if(!is_array($_val)) {
80                     $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
81                 } else {
82                     $smarty->trigger_error("html_input_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
83                 }
84                 break;
85         }
86     }
87
88     if (empty($file)) {
89         $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
90         return;
91     }
92
93     if (substr($file,0,1) == '/') {
94         $_image_path = $basedir . $file;
95     } else {
96         $_image_path = $file;
97     }
98     
99     if(!isset($params['width']) || !isset($params['height'])) {
100         if(!$_image_data = @getimagesize($_image_path)) {
101             if(!file_exists($_image_path)) {
102                 $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
103                 return;
104             } else if(!is_readable($_image_path)) {
105                 $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
106                 return;
107             } else {
108                 $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
109                 return;
110             }
111         }
112         if ($smarty->security &&
113             ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
114             (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
115             (!smarty_core_is_secure($_params, $smarty)) ) {
116             $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
117         }        
118         
119         if(!isset($params['width'])) {
120             $width = $_image_data[0];
121         }
122         if(!isset($params['height'])) {
123             $height = $_image_data[1];
124         }
125
126     }
127
128     if(isset($params['dpi'])) {
129         if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
130             $dpi_default = 72;
131         } else {
132             $dpi_default = 96;
133         }
134         $_resize = $dpi_default/$params['dpi'];
135         $width = round($width * $_resize);
136         $height = round($height * $_resize);
137     }
138
139     return $prefix . '<input type="image" src="'.$path_prefix.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $suffix;
140 }
141
142 /* vim: set expandtab: */
143
144 ?>
This page took 0.060094 seconds and 3 git commands to generate.