Saving Each PDF Page to an Image Using Imagick

后端 未结 4 730
生来不讨喜
生来不讨喜 2020-12-29 07:49

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 func

相关标签:
4条回答
  • 2020-12-29 08:24

    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();
        }
        //}
    ?>
    
    0 讨论(0)
  • 2020-12-29 08:30
     /* 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();
                    }
    
    0 讨论(0)
  • 2020-12-29 08:44

    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.

    0 讨论(0)
  • 2020-12-29 08:47

    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.

    0 讨论(0)
提交回复
热议问题