使用 Imagick-resizeImage 创建缩略图会创建深色图像 - PHP

Using Imagick-resizeImage to create thumbnails creates dark images - PHP

我能够成功地调整(调整到适当的大小)我从 pdf 文档的一页创建的图像。但是,我不明白为什么结果是带有白色补丁的黑色调整大小的图像。拜托,有人可以建议吗?

PHP代码:

 // Create image from first page of pdf document
 $im = new imagick('1Mpublic.pdf[0]');
 $im->setImageFormat('jpg');

 $imageHeight =  $im -> getImageHeight();
 $imageWidth = $im -> getImageWidth();

 $desiredImgWidth = 200;
 $desiredImgHeight = resizedImageHeight($imageWidth, $imageHeight, 
 $desiredImgWidth);

 $im -> resizeImage($desiredImgWidth, $desiredImgHeight, 
 imagick::STYLE_NORMAL, 1);


 // Save image
 $page = '1';
 $saveToFolder = 'thumbnailFolder';
 $fileName = 'thanhThumbNail_'.$page.'.jpg';
 $saveImgToPath = $saveToFolder.'/'.$fileName;
 $result = file_put_contents($saveImgToPath, $im);





 function resizedImageHeight($imgWidth, $imgHeight, $desiredImgWidth){

     $quoient = $imgWidth/$imgHeight;
     $height = $desiredImgWidth/$quoient;

     return $height;
 } 

生成的缩略图:

Link 到原始 PDF 可以在这里找到:

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-04-1091539.pdf

背景颜色似乎没有定义。您需要先设置背景颜色才能阅读PDF文档。

// Create image from first page of pdf document
$im = new imagick();
$im->setBackgroundColor('WHITE');
$im->readImage('1Mpublic.pdf[0]');
$im->setImageFormat('jpg');

您的 PDF 具有透明度。 JPG 不支持透明度,在 PDF 透明的地方显示黑色。只需关闭透明度即可。在 Imagemagick 命令行中:

convert -density 300 ksgt-06-04-1091539.pdf[0] -alpha off result.jpg

请参阅 http://us3.php.net/manual/en/imagick.setimagealphachannel.php

处的 setImageAlphaChannel