我正在尝试使用 php 中的 imagick 将 pdf 文件转换为图像。该脚本工作正常,但创建的图像具有灰色背景,就像 pdf 文件的背景被删除一样
$srcimg = $_SERVER['DOCUMENT_ROOT'] . '/files/pdf_canevas/doc1.pdf';
$targetimg = $_SERVER['DOCUMENT_ROOT'] . '/files/pdf_canevas/doc1.jpeg';
$im = new imagick();
$im->setResolution(150, 150);
$im->readImage($srcimg);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
$im->writeImage($targetimg);
$im->clear();
$im->destroy();
在readImage之后使用这个
$im = $im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN );
更新代码如下
$srcimg = $_SERVER['DOCUMENT_ROOT'] . '/files/pdf_canevas/doc1.pdf';
$targetimg = $_SERVER['DOCUMENT_ROOT'] . '/files/pdf_canevas/doc1.jpeg';
$im = new imagick();
$im->setResolution(150, 150);
$im->readImage($srcimg);
$im = $im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN );
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
$im->writeImage($targetimg);
$im->clear();
$im->destroy();