ImageMagick的缩略图生成与PHP - 使用-crop
-
16-09-2019 - |
题
很久以前,我创建了一个小型图书馆通过system(...)
调整使用ImageMagick的图像,因为我不觉得内置的ImageMagick的功能PHP就足够了。
不过,最近我不得不端口这symfony项目,我选择使用sfThumbnailPlugin(如果我没记错的话)。不幸的是这不包括作物的功能 - 即指定所希望的尺寸,例如300×300像素,并有缩略图裁剪,使其适合。我选择了实现这个功能我自己,但似乎有些不妥。
每当我调整图像大小到所需的大小有WHE宽度大于高度,比例就会上当受骗。看看这个例子: http://i37.tinypic.com/9hkqrl.png - 在本例中的顶行是正确的比例和底行是问题
在该示例中,顶部和底部应已裁剪。
下面是其中所述农作物完成(变量名应该是不言自明的)的部分的代码:
<?php
if ($width/$height > $this->maxWidth/$this->maxHeight) {
// then resize to the new height...
$command .= ' -resize "x'.$this->maxWidth.'"';
// ... and get the middle part of the new image
// what is the resized width?
$resized_w = ($this->maxWidth/$height) * $width;
// crop
$command .= ' -crop "'.$this->maxHeight.'x'.$this->maxWidth.'+'.round(($resized_w - $this->maxWidth)/2).'+0"';
} else {
// or else resize to the new width
$command .= ' -resize "'.$this->maxHeight.'x"';
// ... and get the middle part of the new image
// what is the resized height?
$resized_h = ($this->maxHeight/$width) * $height;
// crop
$command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
'+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
}
时是产生了错误的代码if语句的第二部分。
谁能解决这个给我吗?明显的计算是错误的。
解决方案
的解决方案是以下内容:
if ($width/$height > $this->maxWidth/$this->maxHeight) {
// then resize to the new height...
$command .= ' -resize "x'.$this->maxHeight.'"';
// ... and get the middle part of the new image
// what is the resized width?
$resized_w = ($this->maxHeight/$height) * $width;
// crop
$command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.'+'.round(($resized_w - $this->maxWidth)/2).'+0" +repage';
} else {
$command .= ' -resize "' . $this->maxWidth . 'x"';
$resized_h = ($this->maxWidth/$width) * $height;
// crop
$command .= ' -crop "'.$this->maxWidth.'x'.$this->maxHeight.
'+0+'.round(($resized_h - $this->maxHeight)/2).'" +repage';
}
其他提示
我告诉一个建议,你可以尝试一下,
我看到图片链接我认为条件不正确地检查。
您已经检查条件的 [(宽度/高度)>(maxWidth / maxHeight)] 强>代替检查
如果(宽度==高度){} ELSEIF(宽度>高度){} 否则(宽度<高度){} 强>
检查此条件下,并根据该条件剪裁图像并调整大小。
感谢您
不隶属于 StackOverflow