开发者

find proportional resize dim which should be less than or equal to the needed thumb dim

I php need to resize an image so that both width and height after resize shoul开发者_JAVA技巧d 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?


//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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜