找到比例调整大小昏暗应该小于或等于所需的拇指暗淡

时间:2010-10-06 07:22:44

标签: php image-manipulation

我需要调整图像大小,以便调整大小后的宽度和高度都应小于或等于预期的调整大小值。

考虑源图像是680x520

所需的thumbmail大小应为< = 300x200

我想找到调整后的比例值,该值应小于或等于300x200。

它可以是299x199或300x200但宽度不超过300,高度不超过200。

因此两者都应小于或等于所需的大小。

可能是什么公式?

2 个答案:

答案 0 :(得分:2)

//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);
}

编辑:

//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);
}

我没有找到你给我的逆转的任何例子。如果你有,请告诉我,我会检查以上脚本是否需要修改。

答案 1 :(得分:1)

可能偏离主题,但对于图像处理真的很容易使用: Wide Image

相关问题