Thursday, July 24, 2008

Autosizing Images with PHP

This simple PHP function will show you how to create an auto-sizing imaging system, which is especially handy when users are uploading their own images that may be a 1000 meters wide and high; it prevents people from destroying the layout to your site.
writePicture
Code:
function writePicture($image, $maxwidth, $maxheight)
{
if($image !== "")
{
list($width, $height, $type, $attr) = getimagesize($image);
if($width > $maxwidth)
{
$constant = $maxwidth/$width;
$width = $constant * $width;
$height = $constant * $height;
}
if($height > $height)
{
$constant = $maxheight/$height;
$width = $constant * $width;
$height = $constant * $height;
}
echo"";
}
else
{
if($maxwidth < 1){ $maxwidth = 100; } if($maxheight < 1) { $maxheight = 100;}
echo"";
}
}

No comments: