使用 PHP 将 PDF 转换为 JPG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8624886/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:06:52  来源:igfitidea点击:

PDF to JPG conversion using PHP

phppdfimagemagickimagick

提问by user1016265

I'm trying to convert PDF to IMG (JPG) with help PHP.

我正在尝试借助 PHP 将 PDF 转换为 IMG (JPG)。

I'm using imagick extension.

我正在使用 imagick 扩展。

this is my code

这是我的代码

    $fp_pdf = fopen($pdf, 'rb');

    $img = new imagick(); // [0] can be used to set page number
    $img->readImageFile($fp_pdf);
    $img->setImageFormat( "jpg" );
    $img->setImageCompression(imagick::COMPRESSION_JPEG); 
    $img->setImageCompressionQuality(90); 

    $img->setResolution(300,300);

    $img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);

    $data = $img->getImageBlob(); 

my source pdf file has right dimension (210x297 mm, like A4 has). And everything looks good. But my jpg has page dimension as 842x595 px, and DPI is 72.

我的源 pdf 文件具有正确的尺寸(210x297 毫米,就像 A4 一样)。一切看起来都很好。但是我的 jpg 页面尺寸为 842x595 像素,DPI 为 72。

and img file much more smaller on paper then pdf, when i had print it.

和 img 文件在纸上比 pdf 小得多,当我打印它时。

what is a proper way to make image file from pdf and make it so big as pdf (on paper)

从pdf制作图像文件并使其像pdf一样大(纸上)的正确方法是什么

采纳答案by Alasdair

ImageMagick uses GhostScript to process JPEGs, so you'd do better to execGhostScript directly, which would be much more efficient and give you more control. It would also be only 1 execstatement, instead of playing around with the IMagick functions.

ImageMagick 使用 GhostScript 来处理 JPEG,所以你最好exec直接对 GhostScript 进行处理,这样效率会更高,并且给你更多的控制权。它也只有 1 个exec语句,而不是使用 IMagick 函数。

回答by Jonathan Williamson

You could use imagemagick through exec() or similar, the shell arguments are much less verbose than the PHP extension.

您可以通过 exec() 或类似方法使用 imagemagick,shell 参数比 PHP 扩展要少得多。

$pdf_file = escapeshellarg( "mysafepdf.pdf" );
$jpg_file = escapeshellarg( "output.jpg" );

$result = 0;
exec( "convert -density 300 {$pdf_file} {$jpg_file}", null, $result );

// at this point $result should == 0 if the conversion was successful

It's the "-density" (which sets the DPI to read the source file as) option that specifically fixes your problem.

这是专门解决您的问题的“-密度”(将 DPI 设置为读取源文件)选项。

Also imagemagick by default uses a -quality setting of 92 for JPEG writing in most cases - so you probably don't need to explicitly declare it.

此外,在大多数情况下,imagemagick 默认使用 92 的 -quality 设置进行 JPEG 写入 - 因此您可能不需要明确声明它。

回答by Alfredo Castaneda Garcia

It looks like you missed two setters:

看起来你错过了两个二传手:

Imagick::setImagePage() http://www.php.net/manual/en/function.imagick-setimagepage.php

Imagick::setImagePage() http://www.php.net/manual/en/function.imagick-setimagepage.php

And:

和:

Imagick::setImageExtent() http://www.php.net/manual/en/function.imagick-setimageextent.php

Imagick::setImageExtent() http://www.php.net/manual/en/function.imagick-setimageextent.php

In order to get the correct parameters for these functions, you may try the following:

为了获得这些函数的正确参数,您可以尝试以下操作:

$fp_pdf = fopen($pdf, 'rb');
$params=array();

    $img = new imagick(); 
    $img->readImageFile($fp_pdf);
    /*my modification: */$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
    /*my modification: */$params=$img->identifyImage();
    $img->setImageFormat( "jpg" );
    $img->setImageCompression(imagick::COMPRESSION_JPEG); 
    $img->setImageCompressionQuality(90); 
    /*my modification: */$img->setPage($params['geometry']['width'], $params['geometry']['height'], 0, 0)
    /*my modification: */$img->setResolution($params['resolution']['x'], $params['resolution']['y']);
    $img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
    $data = $img->getImageBlob();

If you find that some others attributes should be set, then let me show you the information that $params is holding. It may proof useful for you:

如果您发现应该设置其他一些属性,那么让我向您展示 $params 保存的信息。它可能证明对您有用:

$params=array(
    [imageName],
    [format], 
    [geometry] => Array
        (
            [width]
            [height] 
        )
    [type], 
    [colorSpace], 
    [resolution],
        (
            [x] 
            [y]
        )

    [units],
    [fileSize],
    [compression],
    [signature], 

)

)

To be honest, I'm not completely sure if this will work. Is just a try in order to help you. I sincerely hope it does.

老实说,我不完全确定这是否有效。只是为了帮助你的尝试。我真诚地希望它。

回答by caugner

As mentioned before, setting the resolution before reading the file does the trick:

如前所述,在读取文件之前设置分辨率可以解决问题:

$fp_pdf = fopen($pdf, 'rb');

$img = new imagick(); // [0] can be used to set page number
$img->setResolution(300,300);
$img->readImageFile($fp_pdf);
$img->setImageFormat( "jpg" );
$img->setImageCompression(imagick::COMPRESSION_JPEG); 
$img->setImageCompressionQuality(90); 

$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);

$data = $img->getImageBlob(); 

回答by Pieter

You have to call setResolution before reading the image. Otherwise imagemagick will use the default system dpi.

您必须在读取图像之前调用 setResolution。否则 imagemagick 将使用默认系统 dpi。