3 * template_lite {html_image} function plugin
7 * Purpose: Outputs an image tag along with resized height/width
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
19 * {html_image url="http://www.yoursite.com/image.jpg"}
20 * {html_image url="images/me.gif" alt="A picture of me!"}
22 * Author: Paul Lockaby <paul@paullockaby.com>
24 function tpl_function_html_image($params, &$tpl)
26 require_once("shared.escape_chars.php");
34 $basedir = isset($_SERVER['DOCUMENT_ROOT']) ?
$_SERVER['DOCUMENT_ROOT'] : '';
35 foreach($params as $_key => $_val)
50 $
$_key = tpl_escape_chars($_val);
54 throw new Template_Exception("html_image: attribute '$_key' cannot be an array", $tpl);
59 $prefix = '<a href="' . $_val . '">';
65 $extra .= ' '.$_key.'="'.template_function_escape_special_chars($_val).'"';
69 throw new Template_Exception("html_image: extra attribute '$_key' cannot be an array", $tpl);
77 throw new Template_Exception("html_image: missing 'url' parameter", $tpl);
81 if (substr($file,0,1) == '/')
83 $_image_path = $basedir . $file;
90 // 0 = width, 1 = height
91 if ($size = @getimagesize
($url))
93 if (empty($limit) ||
$limit == false)
95 // only a height was specified; we will fill in the width
100 // only a width was specified; we will fill in the height
105 // neither a height nor a width was specified; we will fill in both
106 if (empty($width) && empty($height))
114 if ((!empty($width) && ($size[0] > $width)) ||
(!empty($height) && ($size[1] > $height)))
116 if (!empty($height) && !empty($width))
118 // compare the ratios to determine how much each dimension needs to be changed
120 // this will return the width if the height is set to specified
121 $bth_width = round($size[0]*($height/$size[1]));
123 // this will return the height if the width is set to specified
124 $bth_height = round($size[1]*($width/$size[0]));
126 // first we set the width to the max and see how big the height will be
127 if (!($bth_height > $height))
129 // returned height is acceptable (i.e. less than specified)
130 $fin1_height = $bth_height;
131 $fin1_width = $width;
134 // now we set the height to the max and see how big the width will be
135 if (!($bth_width > $width))
137 // returned width is acceptable (i.e. less than specified)
138 $fin2_height = $height;
139 $fin2_width = $bth_width;
142 // check to see if both of them went through
143 if (isset($fin1_height) && isset($fin1_width) && isset($fin2_height) && isset($fin2_width))
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))
149 $new_height = $fin1_height;
150 $new_width = $fin1_width;
154 $new_height = $fin2_height;
155 $new_width = $fin2_width;
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))
163 $new_height = $fin1_height;
164 $new_width = $fin1_width;
168 $new_height = $fin2_height;
169 $new_width = $fin2_width;
175 // only a height or only a width was specified
179 // working with only a height now
180 $new_height = $height;
181 $new_width = round($size[0]*($height/$size[1]));
185 // working with only a width now
186 $new_height = round($size[1]*($width/$size[0]));
191 $height = $new_height;
201 return $prefix . '<img src="' . $url . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"'.$extra.' />' . $suffix;