我一直在努力弄清楚如何在PHP中调整上传图片的大小,使其不会小于给定大小(650x650)。但是,如果用户上传的图片在任一边上都小于我的650最小值,则不会采取任何操作。将图片大小调整为最小宽度/高度
场景1 - 一幅2000像素宽的371像的图像被更新 - 这不会被调整大小,因为371像素已经小于我的最小值。
场景2 - 上传一张2000像素到1823px的图像 - 这里我应该调整图像的大小尽可能接近最小,但不允许宽度或高度低于650像素。
这是我一直沿着到目前为止思考(我使用的是优秀的simpleImage脚本,以帮助调整和获取尺寸)行:
$curWidth = $image->getWidth();
$curHeight = $image->getHeight();
$ratio = $curWidth/$curHeight;
if ($curWidth>$minImageWidth && $curHeight>$minImageHeight)
{
//both dimensions are above the minimum, so we can try scaling
if ($curWidth==$curHeight)
{
//perfect square :D just resize to what we want
$image->resize($minImageWidth,$minImageHeight);
}
else if ($curWidth>$curHeight)
{
//height is shortest, scale that.
//work out what height to scale to that will allow
//width to be at least minImageWidth i.e 650.
if ($ratio < 1)
{
$image->resizeToHeight($minImageWidth*$ratio);
}
else
{
$image->resizeToHeight($minImageWidth/$ratio);
}
}
else
{
//width is shortest, so find minimum we can scale to while keeping
//the height above or equal to the minimum height.
if ($ratio < 1)
{
$image->resizeToWidth($minImageHeight*$ratio);
}
else
{
$image->resizeToWidth($minImageHeight/$ratio);
}
}
但是这给了我一些奇怪的结果,有时它仍然会低于最小值。其中唯一符合预期的部分是对尺寸在最小值以上的测试 - 它不会缩小任何太小的尺寸。
我认为我最大的问题在于,我没有完全理解图像宽高比和尺寸之间的关系,以及如何计算出我能够缩放到哪些尺寸高于最小值。有什么建议么?
我对第一句话感到困惑。 “宽度和高度不小于设定的最小值”...然后它说2000x371不会调整大小,因为它太短。你能澄清你需要的规则吗?或者,也许我误解了你。 – jon 2012-02-27 14:58:21
我会更新,使其更清晰 - 基本上我想重新缩小到650最小宽度和高度的东西。也不应少 - 但如果有人上传的图像已经很小,则不应采取任何行动。 – jammypeach 2012-02-27 15:01:29