简体   繁体   中英

Converting multipage pdf to multi images

I am trying to convert a multipage PDF File to images by using PHP Image magic extension.The problem is that instead of getting images corresponding to each page of the file, I am getting the last page of pdf as the output image. Here is the code:

$handle = fopen($imagePath, "w");
$img1 = new Imagick();

$img1->setResolution(300,300);
$img1->readImage(path to pdf file);
$img1->setColorspace(imagick::IMGTYPE_GRAYSCALE);
$img1->setCompression(Imagick::COMPRESSION_JPEG);
$img1->setCompressionQuality(80);
$img1->setImageFormat("jpg");

$img1->writeImageFile($handle);

What am I doing wrong?The convert command on commandline with the same parameters works.

Try something like this instead:

$images = new Imagick("test.pdf"); 
foreach($images as $i=>$image) {
    $image->setResolution(300,300);
    //etc 
    $image->writeImage("page".$i.".jpg"); 
} 

Try writeImages function. It creates each page as one image and it gives file names for multiple images like this: yourimagename, yourimagename-1, yourimagename-2.... It increases automatically from 0 to your numberofpagesinPdf-1.

The code looks like this:

$imagick = new Imagick($file_handle);
$imagick->readImage();
$imagick->writeImages($yourImagename.'.jpg', false);

Try something like this if you know number of pages of your pdf:

$images = new Imagick(); 

foreach ($pages as $p){

    $im->readImage($PdfFile."[".$p."]");    //yourfile.pdf[0], yourfile.pdf[1], ...

    $im->setCompression(Imagick::COMPRESSION_JPEG); 
    $im->setCompressionQuality(82); 
    $im->setImageFormat( "jpg" );
    //...
    $image_out = "image_".$p.".jpg";
    $im->writeImage($image_out);
}

$im->clear();
$im->destroy();

If you dont know number of pages, you could do something like this:

$images = new Imagick();
$im->readImage($PdfFile);
$pages = (int)$im->getNumberImages();

this worked for me

$file = "./path/to/file/name.pdf";
$fileOpened = @fopen($archivo, 'rb');
$images = new Imagick();
$images->readImageFile($fileOpened);
foreach ($images as $i => $image) {
     $image->setResolution(300, 300);
     $image->setImageFormat("jpg");
     $image->setImageCompression(imagick::COMPRESSION_JPEG);
     $image->setImageCompressionQuality(90);
     $image->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
     $data_blob = $image->getImageBlob();
     $ouput="./path/images/files/page" . $i . ".jpg";
     file_put_contents($ouput, $data_blob);
}
@fclose($fileOpened);

I hope I can help you too

This will work for pdf having multiple pages as well as the single page.

$pdf_file = 'path/to/pdf/file.php';
$image = new imagick();
$image->setResolution(300,300);
$image->readImage($pdf);
$image->setImageFormat('jpg');

// Set all other properties

$pages = $image->getNumberImages();

if ($pages) {
    foreach($image as $index => $pdf_image) {
        $pdf_image->writeImage('destination/path/' . $index . '-image_file.jpg');
    }
} else {
    echo 'PDF doesn\'t have any pages';
}

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