Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Each PDF Page to an Image Using Imagick

I have the following php function below that's converting a local PDF file into images. In short, I want each PDF page to be converted to a separate image.

The function converts the PDF to an image - but only the last page. I want every page of the PDF to be converted to a image and numbered. Not just the last page of the PDF.

Currently, this function converts the last page of example.pdf to example-0.jpg. Issue I'm sure lies within the for method. What am I missing?

$file_name = 'example.pdf'; // using just for this example, I pull $file_name from another function

function _create_preview_images($file_name) {

    // Strip document extension
    $file_name = basename($file_name, '.pdf');

    // Convert this document
    // Each page to single image
    $img = new imagick('uploads/'.$file_name.'.pdf');

    // Set background color and flatten
    // Prevents black background on objects with transparency
    $img->setImageBackgroundColor('white');
    $img = $img->flattenImages();

    // Set image resolution
    // Determine num of pages
    $img->setResolution(300,300);
    $num_pages = $img->getNumberImages();

    // Compress Image Quality
    $img->setImageCompressionQuality(100);

    // Convert PDF pages to images
    for($i = 0;$i < $num_pages; $i++) {         

        // Set iterator postion
        $img->setIteratorIndex($i);

        // Set image format
        $img->setImageFormat('jpeg');

        // Write Images to temp 'upload' folder     
        $img->writeImage('uploads/'.$file_name.'-'.$i.'.jpg');
    }

    $img->destroy();
}
like image 344
Mike Barwick Avatar asked Dec 15 '13 19:12

Mike Barwick


4 Answers

Seems like most of my code was correct. The issue was, I was using $img->flattenImages(); incorrectly. This merges a sequence of images into one image. Much like how Photoshop flattens all visible layers into an image when exporting a jpg.

I removed the above line and the individual files were written as expected.

like image 168
Mike Barwick Avatar answered Oct 18 '22 01:10

Mike Barwick


 /* convert pdf file to list  image files */
                if($_FILES['file_any']['type']=='application/pdf'){
                    $file_name = str_replace(substr($url,0,strpos($url,$_FILES['file_any']['name'])),'',$url);
                    $basename = substr($file_name,0,strpos($file_name,'.'));
                    $abcd = wp_upload_dir();
                    $delpath = $abcd['path'];
                    $savepath = $abcd['url'];
                    $dirpath = substr($savepath,(strpos($savepath,'/upl')+1));

                    $file_name = basename($file_name, '.pdf');
                    $img = new imagick($delpath.'/'.$file_name.'.pdf');

                    $img->setImageBackgroundColor('white');
                    $img->setResolution(300,300);
                    $num_pages = $img->getNumberImages();
                    $img->setImageCompressionQuality(100);
                    $imageurl = NULL;
                    $imagedelurl = NULL;
                    for($i = 0;$i < $num_pages; $i++) {         
                        $imageurl[]=$savepath.'/'.$basename.'-'.$i.'.jpg';
                        $imagedelurl[] = $delpath.'/'.$basename.'-'.$i.'.jpg';
                        // Set iterator postion
                        $img->setIteratorIndex($i);

                        // Set image format
                        $img->setImageFormat('jpeg');

                        // Write Images to temp 'upload' folder     
                        $img->writeImage($delpath.'/'.$file_name.'-'.$i.'.jpg');
                    }
                    $img->destroy();
                }
like image 23
Anand Avatar answered Oct 17 '22 23:10

Anand


There is a much easier way without the loop, just use $img->writeImages($filename,false); and it will make a file per PDF-page. As you said, if you flatten the image first, it only saves 1 page.

like image 22
ChuckThePlant Avatar answered Oct 17 '22 23:10

ChuckThePlant


first install

imagemagick

in your system or server and then create

pdfimage

folder and put pdf file in this folder then run the code and upload it file

<?php
    $file_name = $_FILES['pdfupload']['name']; // using just for this example, I pull $file_name from another function
    //echo strpos($file_name,'.pdf');
    $basename = substr($file_name,0,strpos($file_name,'.'));
    //echo $_FILES['pdfupload']['type'];
    //if (isset($_POST['submit'])){
    if($_FILES['pdfupload']['type']=='application/pdf'){

        // Strip document extension
        $file_name = basename($file_name, '.pdf');
        // Convert this document
        // Each page to single image
        $img = new imagick('pdfimage/'.$file_name.'.pdf');

        // Set background color and flatten
        // Prevents black background on objects with transparency
        $img->setImageBackgroundColor('white');
        //$img = $img->flattenImages();

        // Set image resolution
        // Determine num of pages
        $img->setResolution(300,300);
        $num_pages = $img->getNumberImages();

        // Compress Image Quality
        $img->setImageCompressionQuality(100);
        $images = NULL;
        // Convert PDF pages to images
        for($i = 0;$i < $num_pages; $i++) {         
            $images[]=$basename.'-'.$i.'.jpg';
            // Set iterator postion
            $img->setIteratorIndex($i);

            // Set image format
            $img->setImageFormat('jpeg');

            // Write Images to temp 'upload' folder     
            $img->writeImage('pdfimage/'.$file_name.'-'.$i.'.jpg');
        }
        echo "<pre>";
        print_r($images);
        $img->destroy();
    }
    //}
?>
like image 45
Umesh Maliya Avatar answered Oct 17 '22 23:10

Umesh Maliya