1

我在使用 PHP 和 imagick 创建缩略图时遇到问题。代码工作正常,缩略图以正确的大小生成等,但是当我尝试在缩略图上放置 PDF 徽标时,它变成了半透明。我想这与在 InDesign 中生成的 PDF 文件有关,它可能没有定义任何背景。有没有人遇到过这个问题或知道该怎么做?我试图在背景中放置一块白色的画布,但这并没有帮助。我还为复合图像函数指定了一个通道,但这也无济于事。

这是我遇到问题的 PDF 文件:https ://dl.dropbox.com/u/13712643/Case_Study.pdf 生成的缩略图如下所示: https ://dl.dropbox.com/u/13712643/ Case_Study1.jpg

到目前为止我制作的代码:http://pastebin.com/74CYC972

有任何想法吗?谢谢您的帮助。

4

3 回答 3

1

我有同样的问题,我通过使用在这里找到的Imagick::compositeImage解决了它: php imagick convert PNG to jpg

代码是这样的:

$im = new Imagick();
$im->readimage($pdfFile."[$currentPage]");
$res = $im->getimageresolution();

$bg = new Imagick();
$bg->setresolution($res["x"],$res["y"]); //setting the same image resolution

//create a white background image with the same width and height
$bg->newimage($im->getimagewidth(), $im->getimageheight(), 'white');
$bg->compositeimage($im, Imagick::COMPOSITE_OVER, 0, 0); //merging both images
$bg->setimageformat("png");

//then you can write to a file
$bg->writeImage('white-background-pdf-image.png');

//or output it
header('Content-type: image/png');
echo $bg;
于 2013-09-02T22:04:11.740 回答
0

可能这就是你要找的:

$im->setBackgroundColor(new ImagickPixel('transparent')); 

http://www.php.net/manual/en/imagick.setbackgroundcolor.php

于 2012-10-04T08:05:11.943 回答
0

现有的答案都不适合我。在创建一个新的 imagick() 之后将图像展平:

$im = $im->flattenImages();

编辑: flattenImages 方法已被弃用和删除。利用

$im = $im->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
于 2021-12-02T22:43:36.440 回答