简体   繁体   English

为什么Imagick的缩略图失败?

[英]Why does Imagick fail with my thumbnail?

I try to make a thumbnail image with Imagick. 我尝试用Imagick制作缩略图。 To make the question simpler, I have the following test code: 为了使问题更简单,我有以下测试代码:

//loading picture
$image = new Imagick('/home/ytg/temp/blank.png');
//checking image size
echo "width: {$image->getimagewidth()}\n";
echo "height: {$image->getimageheight()}\n";
//creating thumbnail
$image->thumbnailImage(220, 220, true);
//checking image size
echo "new width: {$image->getimagewidth()}\n";
echo "new height: {$image->getimageheight()}\n";

Which gives me the following result: 这给了我以下结果:

width: 300
height: 300
new width: 219
new height: 220

Why does it make the thumbnail image width a pixel smaller? 为什么将缩略图宽度缩小一个像素? How could I prevent this from happening? 如何防止这种情况发生? I wouldn't like to use the last $fill parameter of thumbnailImage() , because the input image does not always have the same width and height and I wouldn't like if it filled the thumbnail in those cases. 我不想使用thumbnailImage()的最后一个$fill参数,因为输入图像并不总是具有相同的宽度和高度,并且我不希望在这种情况下填充图像。

( PHP Version => 5.4.6-1ubuntu1.1; imagick module version => 3.1.0RC1 ) PHP Version => 5.4.6-1ubuntu1.1; imagick module version => 3.1.0RC1

Right now I'm settling with this solution: 现在,我正在解决以下问题:

$image = new Imagick('/home/ytg/temp/blank.png');
//checking image size
echo "width: {$image->getimagewidth()}\n";
echo "height: {$image->getimageheight()}\n";
//creating thumbnail
$image->thumbnailImage(220, 220, true);
//checking image size
echo "new width: {$image->getimagewidth()}\n";
echo "new height: {$image->getimageheight()}\n";        
$image->thumbnailImage($width, $height, true);                                                
if (($width == $height) && ($image->getImageWidth() != $image->getImageHeight())) {
    $image->thumbnailImage($width, $height, true, true);            
}

It's not perfect though because it works only with the specific case of square images. 但这并不是完美的,因为它仅适用于方形图像的特定情况。 But in that special case it solves my problem. 但是在那种特殊情况下,它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM