使用 PHP 和 ImageMagick 将 PDF 转换为 JPEG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9227014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert PDF to JPEG with PHP and ImageMagick
提问by Leon van der Veen
I'm using a litte script to convert PDF to JPG. That works but the quality is very poor.
我正在使用一个小脚本将 PDF 转换为 JPG。这有效,但质量很差。
The script:
剧本:
$im = new imagick( 'document.pdf[ 0]' );
$im->setImageColorspace(255);
$im->setResolution(300, 300);
$im->setCompressionQuality(95);
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();
One more thing, I want to keep the original size of the PDF but the conversion crops the size of the JPG.
还有一件事,我想保留 PDF 的原始大小,但转换会裁剪 JPG 的大小。
回答by wojtek
It can be done using setResolution
, but you need to do it before loading an image.
Try something like this:
可以使用 完成setResolution
,但您需要在加载图像之前完成。尝试这样的事情:
// instantiate Imagick
$im = new Imagick();
$im->setResolution(300,300);
$im->readimage('document.pdf[0]');
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();
回答by Orbling
The quality of the image produced from the PDF can be changed by setting the density
(which is the DPI) before reading in the PDF - this gets past to ghostscript (gs)
underneath which rasterizes the PDF. To get a good result, supersample at double the density you require, and use resample
to get back to the desired DPI. Remember to change the colorspace
to RGB if you want an RGB JPEG.
可以通过density
在读取 PDF 之前设置(即 DPI)来更改从 PDF 生成的图像的质量- 这将传递到ghostscript (gs)
光栅化 PDF 的下方。要获得良好的结果,请以所需密度的两倍进行超采样,并使用它resample
来恢复所需的 DPI。colorspace
如果您想要 RGB JPEG,请记住将 更改为 RGB。
A typical command line version for convert
might be:
一个典型的命令行版本convert
可能是:
convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.jpg
If you need to crop it, a -shave
command following the resample is usually sensible, if the image is centred within the page.
如果您需要裁剪它,-shave
如果图像在页面内居中,则重采样后的命令通常是明智的。
As for the PHP IMagick extension, well, I never personally use it - so am unsure of how you specify file reading hints to it, but I would hope it is possible.
至于PHP IMagick 扩展,嗯,我从来没有亲自使用它 - 所以我不确定你如何指定文件读取提示,但我希望它是可能的。
回答by user4341845
$im = new imagick();
//this must be called before reading the image, otherwise has no effect
$img->setResolution(200,200);
//read the pdf
$img->readImage("{$pdf_file}[0]");
回答by Sanjay Kumar N S
Click herefor more details. Try this:
单击此处了解更多详情。尝试这个:
HTML
HTML
<html>
<body>
<form action="ConvertPdfToImg.php" enctype="multipart/form-data" method="post" name="f1">
<input id="templateDoc" name="templateDoc" type="file" />
<input type="submit" />
</form>
</body>
</html>
PHP
PHP
$pdfAbsolutePath = __DIR__."/test.pdf";
if (move_uploaded_file($_FILES['templateDoc']["tmp_name"], $pdfAbsolutePath)) {
$im = new imagick($pdfAbsolutePath);
$noOfPagesInPDF = $im->getNumberImages();
if ($noOfPagesInPDF) {
for ($i = 0; $i < $noOfPagesInPDF; $i++) {
$url = $pdfAbsolutePath.'['.$i.']';
$image = new Imagick($url);
$image->setImageFormat("jpg");
$image->writeImage(__DIR__."/".($i+1).'-'.rand().'.jpg');
}
echo "All pages of PDF is converted to images";
}
echo "PDF doesn't have any pages";
}
回答by HoleInVoid
Ensure that the PDF is created with the correct colour profiles, I once had my JPG being very washed out after resizing due to source file was created with wrong colour profile. See also: ImageMagick PDF to JPEG conversion results in green square where image should be
确保使用正确的颜色配置文件创建 PDF,我曾经因为使用错误的颜色配置文件创建源文件而调整大小后,我的 JPG 非常褪色。另请参阅:ImageMagick PDF 到 JPEG 的转换会导致图像应为绿色方块