ordman
ordman

Reputation: 225

pdf to jpg with ImageMagick

Installed on local server imagemagick, but do not understand how to convert ALL pages of pdf file into a single image jpg. And whether it is even possible.

Without specifying the page number only converts the first page

    $im = new imagick( "libs/pdf/files/$Jfile" );
    // convert to jpg
    $im->setImageColorspace(255);
    $im->setCompression(Imagick::COMPRESSION_JPEG);
    $im->setCompressionQuality(60);
    $im->setImageFormat('jpeg');
    //resize
    $im->resizeImage(385, 500, imagick::FILTER_LANCZOS, 1);  
    //write image on server
    $im->writeImage("libs/pdf/files/$JPGfile");
    $im->clear();
    $im->destroy();

Upvotes: 0

Views: 390

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

Two options...

Option 1

To make each page of the PDF come out as a separate JPEG, change the 3rd last line from:

$im->writeImage("libs/pdf/files/$JPGfile");

to something like:

$im->writeImages("z%03d.jpg",false);

Option 2

To make all pages of the PDF come out in a long list, change where the comment says "write image on server" to the following:

// Write on server
$im->resetIterator();
$appended = $im->appendImages(true);
$appended->writeImage("appended.jpg");

Change true to false depending on whether you want a tall list of images or a wide one.

Upvotes: 1

Related Questions