简体   繁体   English

加快PHP Imagick库PDF - > PNG转换(20秒!)

[英]Speed up PHP Imagick library PDF -> PNG conversion (20seconds !)

I am using Imagick to convert the first page of a PDF to a PNG image. 我正在使用Imagick将PDF的第一页转换为PNG图像。 It is working, but it is very slow. 它工作正常,但速度很慢。

A conversion takes between 8 and 20 seconds for a ~100kb PDF. 对于~100kb的PDF,转换需要8到20秒。

Is there a way to speed up the conversion? 有没有办法加快转换?

My code : 我的代码:

$im = new Imagick($url);
$im->setIteratorIndex(0);
$im->setCompression(Imagick::COMPRESSION_LZW);
$im->setCompressionQuality(90);
$im->setImageFormat("png");
$im->writeImage('C:/Shared/test.png');

Is there a way to speed up the convertion? 有没有办法加快转换?

Sure, upgrade the machine with more processing power, more memory and a faster disk. 当然,升级机器具有更强的处理能力,更多的内存和更快的磁盘。

Also it looks like you're using a Windows operating system. 它看起来像你正在使用Windows操作系统。 I suggest you switch to a Linux system and compile the libraries optimized to the computer's architecture its running on. 我建议你切换到Linux系统并编译优化的库,使其运行在计算机的架构上。

In fact , I don't need Imagick library to generate a simple JPG preview of the first page of a PDF, I just need GhostScript. 事实上,我不需要Imagick库来生成PDF的第一页的简单JPG预览,我只需要GhostScript。

Command line conversion with GhostScript is always faster than Imagick (command line or through PHP) 使用GhostScript进行命令行转换总是比Imagick(命令行或通过PHP)更快

Example with a 12 pages and 650kb : 12页和650kb的示例:

  • Command line GhostScript => 0.6 second 命令行GhostScript => 0.6秒
  • Command line Imagick => 0.8 second 命令行Imagick => 0.8秒
  • PHP extension Imagick => 2.22 seconds PHP扩展名Imagick => 2.22秒

With some documents, Imagick takes 20 seconds against 1.5 second with direct GS command line. 对于一些文档,Imagick使用直接GS命令行需要20秒,而1.5秒。

Here is my test script 这是我的测试脚本

   <?php

    $pathToPdf = realpath("in.pdf");

    $pathToJpg = "./out.jpg";
    $pathToPng = "./out.png";

    try
        {

        $time_start = microtime(true);      
        $gsCall = "\"C:\Program Files (x86)\gs\gs9.06\bin\gswin32.exe\" -q -dBATCH -dMaxBitmap=300000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dFirstPage=1 -dLastPage=1 -sOutputFile=\"{0}\" \"{1}\" -c quit";
        $gsCall = str_replace(array('{0}', '{1}'), array($pathToJpg, $pathToPdf), $gsCall); // or use sprintf
        $str = exec($gsCall, $output=array(), $returnValue);
        echo $gsCall.'<br><br>';
        $time_end = microtime(true);
        $time = $time_end - $time_start;        
        if($returnValue == 0)              
            print "<br><br>Conversion OK ".$time;
        else 
            print "<br><br>Conversion failed.<br />".var_dump($output);     

        $time_start = microtime(true);          
        exec('convert "'.$pathToPdf.'[0]" "'.$pathToPng.'"', $output, $returnValue);                        
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        if($returnValue == 0)              
            print "<br><br>Conversion OK ".$time;
        else 
            print "<br><br>Conversion failed.<br />".var_dump($output);

        $time_start = microtime(true);
        $im = new Imagick($pathToPdf);
        $im->setIteratorIndex(0);
        $im->setCompression(Imagick::COMPRESSION_LZW);
        $im->setCompressionQuality(90);
        $im->setImageFormat("png");
        $im->writeImage('\\\\DELL-PC\Shared\test.png');
        //$im->thumbnailImage(200, 0);
        //echo $im;
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        print "<br><br>Conversion OK ".$time;
    }
catch(Exception $e)
{
    echo $e->getMessage();
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM