简体   繁体   中英

Why does Imagick fail with my thumbnail?

I try to make a thumbnail image with 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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