This is a followup question to PHP: if more than ..px resize to
As you can see it resizes if:
if($width > 604 && $height > 453) {
This works fine is both the width and height are large.
However, if $width is over 604, and $height is under 453 (e.g. 604×300)m then this will skip the resize procedure. The opposite is the same(width is under, height is over). Also, if the dimensions on a picture is 500×900, and it gets resized, it gets really ugly resized. any great fix?
Any good suggestion on how I should handle this please?
edit:
$rel_difference = array('width'=>0, 'height'=>0);
if($width > 604 || $height > 453) {
if($width > 604) $rel_difference['width'] = ($width-604)/604;
if($height > 453) $rel_difference['height'] = ($height-453)/453;
asort($rel_difference);
$newwidth = $width/(1+end($rel_difference));
$newheight = $height/(1+end($rel_difference));
$newwidth = round($newwidth);
$newheight = round($newheight);
$jpeg_quality = 90;
$src = "images/users/status/".$org_name;
$path_thumbs = "images/users/status/";
$thumb_path = $path_thumbs . '/' . $newfilename;
switch(exif_imagetype($move_me)) {
case IMAGETYPE_GIF:
$img_r = imagecreatefromgif($src);
break;
case IMAGETYPE_JPEG:
$img_r = imagecreatefromjpeg($src);
break;
case IMAGETYPE_PNG:
$img_r = imagecreatefrompng($src);
break;
default:
echo "{";
echo "error: 'Not a image!'";
echo "}";
exit(0);
break;
}
$dst_r = ImageCreateTrueColor( $newwidth, $newheight );
imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);
imagejpeg($dst_r,$thumb_path,$jpeg_quality);
unlink($move_me);
}
You can do somthing like:
Which would output something like:
Now you know the largest difference to max.
Which will be:
So you can scale using this value proportional.
EDIT
removed unnecessary array_reverse
EDIT2
The image is now proportionally scaled.
Don’t know whether you can use that much decimals places if not use some rounding.