Reputation: 18103
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. 604x300)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 500x900, 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);
}
Upvotes: 1
Views: 128
Reputation: 1546
use Image Processing (ImageMagick)
its very easy here are some examples
Upvotes: 0
Reputation: 72672
You can do somthing like:
$width = 1000;
$height = 900;
$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);
print_r($rel_difference);
Which would output something like:
Array ( width] => 0.65562913907285 [height] => 0.98675496688742 )
Now you know the largest difference to max.
Which will be:
end($rel_difference);
So you can scale using this value proportional.
EDIT
removed unnecessary array_reverse
EDIT2
// now lets calculate the new dimensions based on our previous code
// I divide the dimensions with the largest difference + 1 to get to the new dimensions
$newwidth = $width/(1+end($rel_difference)); // 503.33333333333
$newheight = $height/(1+end($rel_difference)); // 453
The image is now proportionally scaled.
Don't know whether you can use that much decimals places if not use some rounding.
Upvotes: 2
Reputation: 13461
If you want to set a maximum height or width, you need to use ||
instead of &&
in your conditional.
If you want to have images resize nicely, you'll need to scale them down by some factor instead of just resizing them using a constant. Not every image is the same aspect ratio.
Upvotes: 1
Reputation: 13
if($width > 604 && $height > 453) {
is TRUE only if both conditions are TRUE this means otherwise it is FALSE. If I get your question correctly then, if width = 605 and height = 300 then it is FALSE and nothing in the if block will be executed. (PS. 604 is not over 604)
Upvotes: 1
Reputation: 313
Because of you're logic in the statement, both must be true to execute the code. You'll need an OR (||) in there not AND (&&).
if( $X > 500 || $Y > 300 ) {
resize();
}
Upvotes: 1