简体   繁体   中英

Converting PDF to Images using PHP(ghostscript) taking too long

I have a php script working to convert a pdf file to a series of jpeg images.

How the script achieves this:

- Download pdf to local server.
- Create folders to place jpeg images, one folder for large images and one for small images.
- Extract images from pdfs to correct folders.
- Go through each of the files in the large folder and scale images to 1000 width.

Here is a breakdown of the code I am using.

$outputfile = "filename";
$cmd = "wget -q \"$url\" -O books/$outputfile.pdf";
exec($cmd);

if(!is_dir("books/$outputfile")) mkdir("books/$outputfile");
if(!is_dir("books/large/")) mkdir("books/large/");
if(!is_dir("books/large/$outputfile")) mkdir("books/large/$outputfile");

set_time_limit(9000);

......................................................................
/* Skipped Code to figure out with & height of pdf: $width, $height */
......................................................................

/* Extract Images from PDF, once in a large size (first) and another at its original size */
exec("'gs' -o books/large/$outputfile/$outputfile-%06d.jpg -dDEVICEWIDTHPOINTS=$width -dDEVICEHEIGHTPOINTS=$height -dFIXEDMEDIA=true -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -dPDFFitPage=true -dUseCropBox=true -r300 -dJPEGQ=100 -dTextAlphaBits=4 'books/$outputfile.pdf'",$output1);
exec("'gs' -o books/$outputfile/$outputfile-%06d.jpg -dDEVICEWIDTHPOINTS=$width -dDEVICEHEIGHTPOINTS=$height -dFIXEDMEDIA=true -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -dPDFFitPage=true -dUseCropBox=true       -dJPEGQ=100 -dTextAlphaBits=4 'books/$outputfile.pdf'",$output2);

$directory = "/var/www/html/pdf/books/large/$outputfile/";
$d = dir($directory);
chdir($directory);

/* Scale image to be 1000px wide and auto height */
$largewidth = 1000;
$scale = 1000 / intVal($width);
$largeheight = intVal($height) * $scale;
while($entry = $d->read()) {
if($entry != "." && $entry != "..") {
    $size = getimagesize($entry);
    $fp = fopen($entry, "rb");
    if ($size && $fp) {
        $swidth = 1000;
        $scale = 1000 / intVal($size[0]);
        $sheight = intVal(intVal($size[1]) * $scale);

        $dimg = imagecreatetruecolor($swidth, $sheight);
        $simg = imagecreatefromjpeg($entry);

        imagecopyresampled($dimg,$simg,0,0,0,0,$swidth,$sheight,$size[0],$size[1]);
        imagejpeg($dimg,$entry,85);
    }
    else {
        echo "fail";
    }
}
}
$d->close();

The problem is that it takes close to an hour to convert an entire pdf to a series of images. A pdf is generally 300 to 500 pages long.

Is there anything in this code that you guys think I could do more efficiently?

What takes the most time is at the end of the file I go through each image in the large folder and scale it down to 1000 width.

Also, I have limited access to install any new php extensions on this server, so I think imagemagick is out of the question as well.

Thank you

Since you say the majority of the time is spent scaling the image, you probably want to have Ghostscript produce the image at the required size rather than scaling it afterward.

Also scaling a JPEG image will very likely result in artefacts showing up. If you must scale you should avoid JPEG until the final step.

Note that I know nothing about PHP so I can't really comment on the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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