Php Imagick:Pdf 转换为 JPG,质量非常差

Dha*_*ara 1 php pdf jpeg imagick

我在我的项目之一中使用 Imagick 扩展。这对我来说是新的。

以下是我的代码。

                $pdfPath = $config['upload_path'] . '/' . $fileName;
                $im = new imagick();
                $im->setResolution(300, 300);
                $im->readImage($pdfPath);
                $im->setImageFormat('jpeg');
                $im->setImageCompression(imagick::COMPRESSION_JPEG); 
                $im->setImageCompressionQuality(100);
                $im->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
                $im->clear();
                $im->destroy();
Run Code Online (Sandbox Code Playgroud)

这使我的图像质量非常差。所有文本都转换为黑色背景。图像也无法正确显示。请参阅下图,该图是从 PDF 转换而来的。 在此输入图像描述 请帮我。

小智 5

您需要将图像背景颜色设置为白色的选项。在这里我添加了 $im->->flattenImages(); 选项 在这里您可以找到解决方案https://www.binarytides.com/convert-pdf-image-imagemagick-php/

$pdfPath = $config['upload_path'] . '/' . $fileName;
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($pdfPath);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG); 
$im->setImageCompressionQuality(100);
$im->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
$im->clear();
$im->destroy();
Run Code Online (Sandbox Code Playgroud)

$pdfPath = $config['upload_path'] . '/' . $fileName;
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($pdfPath);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG); 
$im->setImageCompressionQuality(100);
// -flatten option, this is necessary for images with transparency, it will produce white background for transparent regions
$im = $im->flattenImages();
$im->writeImage($config['upload_path'] . '/' . str_replace('pdf', 'jpeg', $fileName));
$im->clear();
$im->destroy();
Run Code Online (Sandbox Code Playgroud)

我不确定这对你是否有帮助。