使用PHP和ImageMagick将PDF转换为JPEG

时间:2021-06-12 09:00:39

I'm using a litte script to convert PDF to JPG. That works but the quality is very poor.

我正在使用一个litte脚本将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。

5 个解决方案

#1


38  

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();

#2


6  

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.

在PDF中阅读之前,可以通过设置密度(即DPI)来改变PDF格式的图像质量,这将会通过ghostscript (gs)来将PDF文件化。为了得到一个好的结果,在你需要的密度的两倍上超充足,并且使用重新采样回到想要的DPI。如果需要RGB JPEG,请记住将颜色空间更改为RGB。

A typical command line version for convert might be:

转换的典型命令行版本可能是:

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.

如果需要裁剪它,如果图像集中在页面中,那么resample后面的-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扩展,我个人从未使用过它——所以我不确定如何为它指定文件读取提示,但我希望这是可能的。

#3


4  

$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]");

#4


1  

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转换结果,在绿色方块中图像应该是。

#5


0  

Click here for 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";

}

#1


38  

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();

#2


6  

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.

在PDF中阅读之前,可以通过设置密度(即DPI)来改变PDF格式的图像质量,这将会通过ghostscript (gs)来将PDF文件化。为了得到一个好的结果,在你需要的密度的两倍上超充足,并且使用重新采样回到想要的DPI。如果需要RGB JPEG,请记住将颜色空间更改为RGB。

A typical command line version for convert might be:

转换的典型命令行版本可能是:

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.

如果需要裁剪它,如果图像集中在页面中,那么resample后面的-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扩展,我个人从未使用过它——所以我不确定如何为它指定文件读取提示,但我希望这是可能的。

#3


4  

$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]");

#4


1  

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转换结果,在绿色方块中图像应该是。

#5


0  

Click here for 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";

}