init
[garradin.git] / include / libs / template_lite / plugins / function.html_image.php
1 <?php
2 /**
3 * template_lite {html_image} function plugin
4 *
5 * Type: function
6 * Name: html_image
7 * Purpose: Outputs an image tag along with resized height/width
8 * Input:
9 * - url = the url of the picture
10 * - width = optional width
11 * - height = optional height
12 * - limit = boolean - will resize image to the above height
13 * and width if the above height and width are
14 * smaller than the real height and width
15 * - border = optional size of the border, default is "0"
16 * - alt = optional alternate text to display
17 * Examples:<br>
18 * <pre>
19 * {html_image url="http://www.yoursite.com/image.jpg"}
20 * {html_image url="images/me.gif" alt="A picture of me!"}
21 * </pre>
22 * Author: Paul Lockaby <paul@paullockaby.com>
23 */
24 function tpl_function_html_image($params, &$tpl)
25 {
26 require_once("shared.escape_chars.php");
27 $alt = '';
28 $url = '';
29 $height = '';
30 $width = '';
31 $extra = '';
32 $prefix = '';
33 $suffix = '';
34 $basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
35 foreach($params as $_key => $_val)
36 {
37 switch($_key)
38 {
39 case 'url':
40 case 'height':
41 case 'width':
42 $$_key = $_val;
43 break;
44 case 'limit':
45 $$_key = true;
46 break;
47 case 'alt':
48 if(!is_array($_val))
49 {
50 $$_key = tpl_escape_chars($_val);
51 }
52 else
53 {
54 throw new Template_Exception("html_image: attribute '$_key' cannot be an array", $tpl);
55 }
56 break;
57 case 'link':
58 case 'href':
59 $prefix = '<a href="' . $_val . '">';
60 $suffix = '</a>';
61 break;
62 default:
63 if(!is_array($_val))
64 {
65 $extra .= ' '.$_key.'="'.template_function_escape_special_chars($_val).'"';
66 }
67 else
68 {
69 throw new Template_Exception("html_image: extra attribute '$_key' cannot be an array", $tpl);
70 }
71 break;
72 }
73 }
74
75 if (empty($url))
76 {
77 throw new Template_Exception("html_image: missing 'url' parameter", $tpl);
78 return;
79 }
80
81 if (substr($file,0,1) == '/')
82 {
83 $_image_path = $basedir . $file;
84 }
85 else
86 {
87 $_image_path = $file;
88 }
89
90 // 0 = width, 1 = height
91 if ($size = @getimagesize($url))
92 {
93 if (empty($limit) || $limit == false)
94 {
95 // only a height was specified; we will fill in the width
96 if (!empty($height))
97 {
98 $width = $size[0];
99 }
100 // only a width was specified; we will fill in the height
101 if (!empty($width))
102 {
103 $height = $size[1];
104 }
105 // neither a height nor a width was specified; we will fill in both
106 if (empty($width) && empty($height))
107 {
108 $width = $size[0];
109 $height = $size[1];
110 }
111 }
112 else
113 {
114 if ((!empty($width) && ($size[0] > $width)) || (!empty($height) && ($size[1] > $height)))
115 {
116 if (!empty($height) && !empty($width))
117 {
118 // compare the ratios to determine how much each dimension needs to be changed
119
120 // this will return the width if the height is set to specified
121 $bth_width = round($size[0]*($height/$size[1]));
122
123 // this will return the height if the width is set to specified
124 $bth_height = round($size[1]*($width/$size[0]));
125
126 // first we set the width to the max and see how big the height will be
127 if (!($bth_height > $height))
128 {
129 // returned height is acceptable (i.e. less than specified)
130 $fin1_height = $bth_height;
131 $fin1_width = $width;
132 }
133
134 // now we set the height to the max and see how big the width will be
135 if (!($bth_width > $width))
136 {
137 // returned width is acceptable (i.e. less than specified)
138 $fin2_height = $height;
139 $fin2_width = $bth_width;
140 }
141
142 // check to see if both of them went through
143 if (isset($fin1_height) && isset($fin1_width) && isset($fin2_height) && isset($fin2_width))
144 {
145 // now check the difference between abs($fin1_height-$fin1_width) and abs($fin2_height-$fin2_width)
146 // since we obviously want the larger image, take whichever one has the smaller difference
147 if (abs($fin1_height - $fin1_width) < abs($fin2_height - $fin2_width))
148 {
149 $new_height = $fin1_height;
150 $new_width = $fin1_width;
151 }
152 else
153 {
154 $new_height = $fin2_height;
155 $new_width = $fin2_width;
156 }
157 }
158 else
159 {
160 // since $new_height and $new_width weren't set above, we have to set them here
161 if (isset($fin1_height) && isset($fin1_width))
162 {
163 $new_height = $fin1_height;
164 $new_width = $fin1_width;
165 }
166 else
167 {
168 $new_height = $fin2_height;
169 $new_width = $fin2_width;
170 }
171 }
172 }
173 else
174 {
175 // only a height or only a width was specified
176 // much easier
177 if (!empty($height))
178 {
179 // working with only a height now
180 $new_height = $height;
181 $new_width = round($size[0]*($height/$size[1]));
182 }
183 if (!empty($width))
184 {
185 // working with only a width now
186 $new_height = round($size[1]*($width/$size[0]));
187 $new_width = $width;
188 }
189 }
190 $width = $new_width;
191 $height = $new_height;
192 }
193 else
194 {
195 $width = $size[0];
196 $height = $size[1];
197 }
198 }
199 }
200
201 return $prefix . '<img src="' . $url . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"'.$extra.' />' . $suffix;
202 }
203 ?>