Resizing
|
|
|
|
<? // this function scales an image if it's bigger then the maxwidth or the maxheigth
function scale($img,$maxwidth,$maxheight) {
$imginfo = getimagesize($img);
$imgwidth = $imginfo[0];
$imgheight = $imginfo[1];
if ($imgwidth > $maxwidth) {
$ration = $maxwidth/$imgwidth;
$newwidth = round($imgwidth*$ration);
$newheight = round($imgheight*$ration);
if ($newheight > $maxheight) {
$ration = $maxheight/$newheight;
$newwidth = round($newwidth*$ration);
$newheight = round($newheight*$ration);
return array("image" => $img, "width" => $newwidth, "height" => $newheight);
} else {
return array("image" => $img, "width" => $newwidth, "height" => $newheight);
}
} else if ($imgheight > $maxheight) {
$ration = $maxheight/$imgheight;
$newwidth = round($imgwidth*$ration);
$newheight = round($imgheight*$ration);
if ($newwidth > $maxwidth) {
$ration = $maxwidth/$newwidth;
$newwidth = round($newwidth*$ration);
$newheight = round($newheight*$ration);
return array("image" => $img, "width" => $newwidth, "height" => $newheight);
} else {
return array("image" => $img, "width" => $newwidth, "height" => $newheight);
}
} else {
return array("image" => $img, "width" => $newwidth, "height" => $newheight);
}
} ?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|