views:

31

answers:

2

I php need to resize an image so that both width and height after resize should be less than or equal to the expected resized values.

consider the source image is 680x520

The needed thumbmail size should be <= 300x200

I want to find the resized proportional value which should be less than or equal to 300x200.

it can be like 299x199 or 300x200 but not greater than 300 in width and 200 in height.

So both should be less than or equal to the needed size.

What could be the formula?

+2  A: 
//height - original image height;
//width  - original image width;
if ($height > $width) {
  $thumb_height = 200;
  $thumb_width = round(($width * $thumb_height) / $height);
} else {
  $thumb_width = 300;
  $thumb_height = round(($height * $thumb_width) / $width);
}

EDIT:

//height - original image height;
//width  - original image width;
$thumb_height = 0;
$thumb_width  = 0;
if ($height > $width) {
    $thumb_height = 200;
} else {
  if ($height > ($width * 200)/300)
    $thumb_height = 200;
  else
    $thumb_width  = 300;
}

if ($thumb_height > 0) {
  $thumb_width = round(($width * $thumb_height) / $height);
} else {
  $thumb_height = round(($height * $thumb_width) / $width);
}

I didn't find any example for the reversed that you gave me. If you have one, please tell me and I will check to see if the above script needs any modifications.

Parkyprg
your formula is not working completely. if width is 630 and height is 500 of the original image then the output is 300x238. But my condition is 238 should be less than 200. if this is getting reduced then proportionally the width should also reduce. consider if we reduce 38px in height then in width also should be reduced with 38px to make it proportional. i hope you can understand what i am expecting.
Jayapal Chandran
I changed the script. Check it.
Parkyprg
If you want to exclude the decimals, you can use floor instead of round.
Parkyprg
yes. excellent. now i got what i wanted. when i checked with photo shop both the calculations were equal. thank you. what you gave is working well when compared with photoshop. i checked with the IMAGE SIZE dialog box (Photoshop -> Image -> Image Size). when resized according to width and when resized according to height both the calcluations are perfectly equal. i tried with odd values 1234x789 and 891x1324. the results were as expected.
Jayapal Chandran
You have said in your edit that you did not find any example for reverse of what i thought. Your formula is the example of what i thought so you can consider yours is the reversed of what i thought. haha. anyway. you can check with photo shop. and if you find any difference then please post here so that i will update it. thank you.
Jayapal Chandran
A: 

Maybe offtopic, but for image manipulation realy easy to use: Wide Image

Wolfy