*
* {html_image url="http://www.yoursite.com/image.jpg"}
* {html_image url="images/me.gif" alt="A picture of me!"}
*
* Author: Paul Lockaby
*/
function tpl_function_html_image($params, &$tpl)
{
require_once("shared.escape_chars.php");
$alt = '';
$url = '';
$height = '';
$width = '';
$extra = '';
$prefix = '';
$suffix = '';
$basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
foreach($params as $_key => $_val)
{
switch($_key)
{
case 'url':
case 'height':
case 'width':
$$_key = $_val;
break;
case 'limit':
$$_key = true;
break;
case 'alt':
if(!is_array($_val))
{
$$_key = tpl_escape_chars($_val);
}
else
{
throw new Template_Exception("html_image: attribute '$_key' cannot be an array", $tpl);
}
break;
case 'link':
case 'href':
$prefix = '';
$suffix = '';
break;
default:
if(!is_array($_val))
{
$extra .= ' '.$_key.'="'.template_function_escape_special_chars($_val).'"';
}
else
{
throw new Template_Exception("html_image: extra attribute '$_key' cannot be an array", $tpl);
}
break;
}
}
if (empty($url))
{
throw new Template_Exception("html_image: missing 'url' parameter", $tpl);
return;
}
if (substr($file,0,1) == '/')
{
$_image_path = $basedir . $file;
}
else
{
$_image_path = $file;
}
// 0 = width, 1 = height
if ($size = @getimagesize($url))
{
if (empty($limit) || $limit == false)
{
// only a height was specified; we will fill in the width
if (!empty($height))
{
$width = $size[0];
}
// only a width was specified; we will fill in the height
if (!empty($width))
{
$height = $size[1];
}
// neither a height nor a width was specified; we will fill in both
if (empty($width) && empty($height))
{
$width = $size[0];
$height = $size[1];
}
}
else
{
if ((!empty($width) && ($size[0] > $width)) || (!empty($height) && ($size[1] > $height)))
{
if (!empty($height) && !empty($width))
{
// compare the ratios to determine how much each dimension needs to be changed
// this will return the width if the height is set to specified
$bth_width = round($size[0]*($height/$size[1]));
// this will return the height if the width is set to specified
$bth_height = round($size[1]*($width/$size[0]));
// first we set the width to the max and see how big the height will be
if (!($bth_height > $height))
{
// returned height is acceptable (i.e. less than specified)
$fin1_height = $bth_height;
$fin1_width = $width;
}
// now we set the height to the max and see how big the width will be
if (!($bth_width > $width))
{
// returned width is acceptable (i.e. less than specified)
$fin2_height = $height;
$fin2_width = $bth_width;
}
// check to see if both of them went through
if (isset($fin1_height) && isset($fin1_width) && isset($fin2_height) && isset($fin2_width))
{
// now check the difference between abs($fin1_height-$fin1_width) and abs($fin2_height-$fin2_width)
// since we obviously want the larger image, take whichever one has the smaller difference
if (abs($fin1_height - $fin1_width) < abs($fin2_height - $fin2_width))
{
$new_height = $fin1_height;
$new_width = $fin1_width;
}
else
{
$new_height = $fin2_height;
$new_width = $fin2_width;
}
}
else
{
// since $new_height and $new_width weren't set above, we have to set them here
if (isset($fin1_height) && isset($fin1_width))
{
$new_height = $fin1_height;
$new_width = $fin1_width;
}
else
{
$new_height = $fin2_height;
$new_width = $fin2_width;
}
}
}
else
{
// only a height or only a width was specified
// much easier
if (!empty($height))
{
// working with only a height now
$new_height = $height;
$new_width = round($size[0]*($height/$size[1]));
}
if (!empty($width))
{
// working with only a width now
$new_height = round($size[1]*($width/$size[0]));
$new_width = $width;
}
}
$width = $new_width;
$height = $new_height;
}
else
{
$width = $size[0];
$height = $size[1];
}
}
}
return $prefix . '' . $suffix;
}
?>