I think this is the algorithm I've used before. It's just about comparing the ratio of the dimensions you want, to the ratio of the dimensions you have:
goalRatio = 80/60;
actualRatio = w/h;
create a thumbnail
fill it with black
if actualRatio > goalRatio // if the image is wider
reduce image to the width (80)
center it vertically
else if actualRatio < goalRatio // if the image is narrower
reduce image to the height (80)
center it horizontally
else
// the ratio matches
scale the image
position in the upper left corner
place the image
Sorry - no time to write the actual code.
Main Topics
Browse All Topics





by: secuteamersPosted on 2006-12-20 at 01:08:39ID: 18172128
Extra info: this is my function:
$doelhoogt e,$doelbre edte,$file name,$dest inationpat h,$filetyp e){
stand);
tand);
webreedte, $nieuwehoogte); ation, $image, 0, 0, 0, 0, $doelbreedte, $doelhoogte, $bronbreedte, $bronhoogte);
npath$file name");
npath$file name");
function createimgcopy($tmpbestand,
$dimensions = getimagesize($tmpbestand);
$bronbreedte = $dimensions[0];
$bronhoogte = $dimensions[1];
if($bronbreedte > $bronhoogte){
$factor = $bronbreedte/$doelbreedte;
$nieuwebreedte = $doelbreedte;
$nieuwehoogte = ($bronhoogte/$factor);
$nieuwehoogte = round($nieuwehoogte, 0);
}else{
$factor = $bronhoogte/$doelhoogte;
$nieuwehoogte = $doelhoogte;
$nieuwebreedte = ($bronbreedte/$factor);
$nieuwebreedte = round($nieuwebreedte, 0);
}
switch($filetype){
case "image/pjpeg":
$image = imagecreatefromjpeg($tmpbe
break;
case "image/x-png":
$image = imagecreatefrompng($tmpbes
break;
}
$destination = imagecreatetruecolor($nieu
imagecopyresampled($destin
//opslag
switch($filetype){
case "image/pjpeg":
ImageJpeg ($destination,"$destinatio
break;
case "image/x-png":
ImageJpeg ($destination,"$destinatio
break;
}
imagedestroy($image);
}