0

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.

vincent
  • 1,243
  • 4
  • 15
  • 29
  • "the text on the images is of very poor quality." - please check my answer to http://stackoverflow.com/a/23144243/778719 – Danack Apr 19 '14 at 19:51

1 Answers1

2

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....

KenS
  • 30,202
  • 3
  • 34
  • 51
  • 1
    Image Magick actually uses Ghostscript to render PDF's - so going directly to Ghostscript should give the same output, but possibly faster. And yeah, +lots for using PNG. – Danack Apr 19 '14 at 19:50
  • The reason i'm not using png as output is the speed, when converting to png seems to be loads slower – vincent Apr 22 '14 at 16:45
  • As far as I know the performance of the PNG driver should be comparable to the JPEG driver, but I've not tested it. – KenS Apr 22 '14 at 20:57