如何使用Imagick,ImageMagick和PHP从PDF创建特定数量的缩略图? [英] How can I create a specific number of thumbnails from a PDF using Imagick, ImageMagick, and PHP?

查看:138
本文介绍了如何使用Imagick,ImageMagick和PHP从PDF创建特定数量的缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP,ImageMagick和Imagick(PHP类)来创建PDF的缩略图。有些PDF只有一页,有些会有很多。我想指定要创建的数量(N)的缩略图,一个用于PDF中前N个页面中的每个页面。

I'd like to use PHP, ImageMagick, and Imagick ( PHP Class ) to create thumbnails of a PDF. Some PDFs will only have one page, and some will have many. I'd like to specify a number ( N ) of thumbnails to create, one for each of the first N pages in the PDF.

下面的代码有效,但产生PDF没有足够的页面时出错(因为我试图在不存在的PDF页面上实例化Imagick)。我也知道ImageMagick可以在从命令行使用时为PDF中的所有页面创建缩略图。我想以最有效的方式(内存和处理器)模仿这种行为,同时控制创建的拇指的数量......我只需要大型PDF中前N页的缩略图。

The code below works, but produces errors when the PDF doesn't have enough pages ( because I'm trying to instantiate Imagick on a PDF page that doesn't exist ). I also know that ImageMagick can create thumbnails for all the pages in a PDF when used from the command line. I'd like to mimic that behavior in the most efficient way ( memory and processor ), while controlling the number of thumbs created ... I only need a thumbnail for the first N pages in large PDFs.

我已经研究了各种方法来确定PDF中的页数,它们似乎都是资源密集型的。也许下面的代码是我最好的选择。它实现了我的目标,但困扰我,因为它会导致一些错误......即使它们不影响最终结果。

I've looked at various methods to determine the number of pages in a PDF, and they all seem to be resource intensive. Perhaps the code below is my best bet. It accomplishes my goal, but bothers me because it results in some errors ... even though they don't affect the ultimate outcome.

这是我目前正在运行的代码(当PDF少于N页时出错):

Here is my code that currently works ( with errors when the PDF has fewer than N pages ):

private function create_thumbnails(
    $num_thumbs = 3     // how many thumbnails to create, defaults to one
) {

    echo "\n\n    creating thumbnails ... ";

    $num_thumbs_created = 0;
    while( $num_thumbs_created < $num_thumbs )
    {

        try {

            // instantiate imagick with the pdf
            $Image = new Imagick( $this->file_path_pdf . '[' . $num_thumbs_created . ']' );

            // define image file
            $Image->setImageColorspace( 255 );
            $Image->setCompression( Imagick::COMPRESSION_JPEG );
            $Image->setCompressionQuality( 60 );
            $Image->setImageFormat( 'jpg' );

            // size the thumbnail
            //  - resized relative to 8.5x11 ( assuming most pdfs are paper sized )
            $Image->resizeImage( 180, 232, imagick::FILTER_POINT, 1 );

            // save image
            $Image->writeImage( substr( $this->File->path, 0, -4 ) . '-' . $num_thumbs_created . '.jpg' );
            $Image->clear();

            $num_thumbs_created++;

        } catch( Exception $e ) {

            echo "\n  * failed to create some or all thumbnails: " . $e->getMessage();
            break;

        }

    }   

    $Image->destroy();

    echo "done";

}


推荐答案

我很漂亮确定如果您打开PDF,那么 Imagick :: getNumberImages 将为您提供页数。同样在那时你可以用magick迭代它们而不必每页实例化。所有这些都是基于文档的推测。所以你必须自己测试并找出:

Im pretty sure if you open a PDF then Imagick::getNumberImages will give you the number of pages. Likewise at that point you can iterate over them with magick without have to instantiate per page. All this is speculation based on the documentation though. So youll have to test and find out for yourself:

private function create_thumbnails( $num_thumbs = 3) {

    echo "\n\n    creating thumbnails ... ";

    try {
        $Image = new Imagick( $this->file_path_pdf);
        $nbCreated = 0;
        if($num_thumbs > 0) {
           foreach($Image as $idx => $im) {
              if($nbCreated < $num_thumbs) {
                $im->setImageColorspace( 255 );
                $im->setCompression( Imagick::COMPRESSION_JPEG );
                $im->setCompressionQuality( 60 );
                $im->setImageFormat( 'jpg' );

                $im->resizeImage( 180, 232, imagick::FILTER_POINT, 1 );

                // save image
                $im->writeImage( $idx . '-' . $nbCreated . '.jpg' );
                $im->clear();

                $nbCreated++;
              }
              else 
              {
                 break; // pop out of loop we have reach our limit and are done
              }
           }

        }

        $Image->destroy();
    }
    catch( Exception $e ) {
       echo "\n  * failed to create some or all thumbnails: " . $e->getMessage();
       $Image->destroy();    
    }   

    echo "done";
}

注意我也改变了你的try / catch的位置。如果出现错误,IMO最好保释并停止一起创建图像,因为如果处理单个文件,则每次迭代都会抛出错误,因此停止第一个异常可能更有效。你可以轻松地将它包装在一个不同的位置,这就是我将如何做到这一点。

Note i also changed where you have your try/catch. IMO its better to bail and stop creating images all together if there is an error because if youre dealing with a single file chances are the error will be thrown on each iteration, so its probably more efficient to stop on the first exception. You could easily wrap it in a different location, thats just how i would do it.

这篇关于如何使用Imagick,ImageMagick和PHP从PDF创建特定数量的缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆