Question

I'm trying to find the fastest way to convert a multiple pdf in to jpeg images without quality loss. I have the following code:

    $this->imagick = new \Imagick();
    $this->imagick->setresolution(300,300);
    $this->imagick->readimage($this->uploadPath . '/original/test.pdf');
    $num_pages = $this->imagick->getNumberImages();

    for($i = 0;$i < $num_pages; $i++) {

        $this->imagick->setIteratorIndex($i);
        $this->imagick->setImageFormat('jpeg');
        $this->imagick->readimage($asset);

        // i need the width, height and filename of each image to add
        // to the DB
        $origWidth = $this->imagick->getImageWidth();
        $origHeight = $this->imagick->getImageHeight();
        $filename = $this->imagick->getImageFilename();


        $this->imagick->writeImage($this->uploadPath . '/large/preview-' . $this->imagick->getIteratorIndex() . '.jpg');

        $this->imagick->scaleImage($origWidth / 2, $origHeight / 2);
        $this->imagick->writeImage($this->uploadPath . '/normal/preview-' . $this->imagick->getIteratorIndex() . '.jpg');

        //add asset to the DB with the given width, height and filename ...
     }

This is very slow thou, partially because the resolution is so big but is i dont add it, the text on the images is of very poor quality. Also, the fact that i'm saving the image first, and then also saving a smaller version of the file is probably not very optimized.

So does anyone have a better method of doing this? Maybe with only ghostscript?

The minimum requirements are that i need 2 versions of the converted image. A real size version and a version at half size. And i need the width and height and filename to add to the database.

Was it helpful?

Solution

You can use Ghostscript, if you set "-sDEVICE=jpeg -sOutputFile=out%d.jpg" then each page will be written to a separate file.

Note that its not really possible to render a PDF to JPEG 'without quality loss' since JPEG is a lossy compression method. Have you considered using a lossless format instead, such as PNG or TIFF ?

To get the same images at different sizes you will need to render the file twice at different resolutions, you set resolution in Ghostscript with '-r'. The width and height can be read easily enough from the image file, or you can use pdf_info.ps to get each page size. Note that a PDF file can contain multiple pages of different sizes, I hope you aren't expecting them all to be the same....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top