PDF到JPG转换使用PHP

时间:2022-03-24 00:18:23

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 px, 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格式(在纸上)?

5 个解决方案

#1


5  

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

ImageMagick使用GhostScript来处理jpeg,所以您最好直接执行GhostScript,这样会更有效,也会给您更多的控制。它也只有一个exec语句,而不是使用IMagick函数。

#2


10  

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.

是“-density”(将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对JPEG编写使用的-quality设置是92,因此您可能不需要显式地声明它。

#3


8  

It looks like you missed two setters:

看起来你漏掉了两个setter:

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.

老实说,我不完全确定这是否行得通。只是想帮你一个忙。我真诚地希望它能。

#4


5  

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

#5


1  

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

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

#1


5  

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

ImageMagick使用GhostScript来处理jpeg,所以您最好直接执行GhostScript,这样会更有效,也会给您更多的控制。它也只有一个exec语句,而不是使用IMagick函数。

#2


10  

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.

是“-density”(将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对JPEG编写使用的-quality设置是92,因此您可能不需要显式地声明它。

#3


8  

It looks like you missed two setters:

看起来你漏掉了两个setter:

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.

老实说,我不完全确定这是否行得通。只是想帮你一个忙。我真诚地希望它能。

#4


5  

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

#5


1  

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

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