在 pdf 缩略图创建中显示自定义错误消息 - PHP - Imagick

发布于 2024-11-17 14:15:33 字数 625 浏览 9 评论 0原文

我在 Windows PC 上运行带有 PHP5 的 Apache。我已经成功配置了 Ghostscript 和 Image magic。我有一个生成 pdf 缩略图的脚本,如下所示;

<?php
$file = 'test.pdf';
$page = 1;
$pdfpage = $page - 1;
$nh = 200;
$nw = 200;

$im = new imagick(realpath($file)."[$pdfpage]");
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->sampleImage($nw,$nh);
$im->writeImage("test.jpg");
echo "true";
?>

这对我来说非常有用。该脚本生成与 $page 变量相对应的页面缩略图。但如果页码超过文档中的可用页数,则此操作将不起作用。我想要的是如果输入变量超过 PDF 文件中的可用页数或者 imagick 函数运行起来有些困难,则显示或回显 "false" 。我该怎么做?

I am running Apache with PHP5 in my windows PC. I have successfully configured Ghostscript and Image magic. I have a script to generate pdf thumbnail as follows;

<?php
$file = 'test.pdf';
$page = 1;
$pdfpage = $page - 1;
$nh = 200;
$nw = 200;

$im = new imagick(realpath($file)."[$pdfpage]");
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat("jpeg");
$im->sampleImage($nw,$nh);
$im->writeImage("test.jpg");
echo "true";
?>

This works great for me. The script generates a page thumbnail corresponding to $page variable. But this wont work if the page number exceeds available number of pages in the document. What I want is to display or echo "false" if the input variable exceeds available number of pages in the PDF file or if the imagick function feel some difficulty to run. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2024-11-24 14:15:33

理论中,您可以使用identify命令的-format参数的%n变量来查找运行主命令之前给定文件(包括 PDF)的页数(“帧”、“图像”)。这样您就可以使用结果将正确的值填充到主命令中,这样您就不会首先收到错误消息:

identify -format %n some.pdf

实践中,这可以是非常慢(因为 ImageMagick 似乎首先渲染完整的 PDF 以计算页数)。

因此,我的建议是使用外部命令行工具来获取 PDF 页数:pdfinfo。该工具专门用于 PDF 文件,它知道在哪里查找该信息(因为该数字是任何 PDF 文件所需元数据的一部分,pdfinfo 不需要先渲染每个页面,然后才能吐出该信息)信息):

pdfinfo some.pdf | grep Pages:
pdfinfo some.pdf | grep Pages: | awk '{print $2}'

为了让您看到这两个命令的不同性能级别,我确实针对包含 PDF-1.7 的官方 ISO 32000 规范的文件运行了它:

time identify  -format %n ~/Downloads/PDF32000_2008.pdf 
756

real  0m51.902s
user  0m50.133s
sys   0m1.090s


time pdfinfo ~/Downloads/PDF32000_2008.pdf | grep Pages: | awk '{print $2}'
756

real  0m0.138s
user  0m0.041s
sys   0m0.016s

因此 756 页 PDF 文档的性能差异是 51.9 秒vs. 0.138 秒(或 376:1)。

我还使用 12 页的 PDF 文件进行了测试。这里的性能比是31:1。一页 PDF 显示
10:1 —— 全部支持 pdfinfo

古老的 IT 口号:“使用正确的工具完成工作......”

In theory, you can use the %n variable to the -format parameter to the identify command to find the number of pages ('frames', 'images') of a given file, including PDF, before you run your main command. This way you can use the result to fill the correct value into your main command, so you don't get the error message in the first place:

identify -format %n some.pdf

In practice, this can be extremely slow (because ImageMagick seems to render the complete PDF virtually first to count the pages).

Therefor, my advice is to use an external commandline tool to get the number of PDF pages: pdfinfo. This tool is specialized on PDF files, and it knows where to look for this info (because this number is part of the required metadata of any PDF file, pdfinfo doesn't need to render each and every page first before it can spit out the info):

pdfinfo some.pdf | grep Pages:
pdfinfo some.pdf | grep Pages: | awk '{print $2}'

Just so you see the different performance levels of the two commands, I did run it against the file that holds the official ISO 32000 spec for PDF-1.7:

time identify  -format %n ~/Downloads/PDF32000_2008.pdf 
756

real  0m51.902s
user  0m50.133s
sys   0m1.090s


time pdfinfo ~/Downloads/PDF32000_2008.pdf | grep Pages: | awk '{print $2}'
756

real  0m0.138s
user  0m0.041s
sys   0m0.016s

So the performance difference for a 756-page PDF document is 51.9 seconds vs. 0.138 seconds (or 376:1).

I also tested with a 12-page PDF file. Here the performance ratio is 31:1. A 1-page PDF shows
10:1 -- all in favor of pdfinfo.

Old IT mantra: 'Use the right tool for the job...'

疧_╮線 2024-11-24 14:15:33
$file        = 'test.pdf';
$total_pages = 1; // Probably should already know pages but if you don't, you can find out with imagick
$pdfpage     = $page - 1;
$nh          = 200;
$nw          = 200;

try {
    for($current_page = $total_pages; $current_page >= 0;$current_page--) {
        $im = new imagick(realpath($file)."[$pdfpage]");
        $im->setCompression(Imagick::COMPRESSION_JPEG);
        $im->setCompressionQuality(100);
        $im->setImageFormat("jpeg");
        $im->sampleImage($nw,$nh);
        $im->writeImage("test.jpg");
        if ($im->haspreviousimage()) { // Imagick reads pages in reverse order, thats why we're getting the previous image
            $im->previousimage();
        } else {
            break;
        }
    }
} catch (ImagickException $e) {
    die('ImagickException ' . $e->getMessage());
} catch (Exception $e) {
    die('Exception ' . $e->getMessage());
}
$file        = 'test.pdf';
$total_pages = 1; // Probably should already know pages but if you don't, you can find out with imagick
$pdfpage     = $page - 1;
$nh          = 200;
$nw          = 200;

try {
    for($current_page = $total_pages; $current_page >= 0;$current_page--) {
        $im = new imagick(realpath($file)."[$pdfpage]");
        $im->setCompression(Imagick::COMPRESSION_JPEG);
        $im->setCompressionQuality(100);
        $im->setImageFormat("jpeg");
        $im->sampleImage($nw,$nh);
        $im->writeImage("test.jpg");
        if ($im->haspreviousimage()) { // Imagick reads pages in reverse order, thats why we're getting the previous image
            $im->previousimage();
        } else {
            break;
        }
    }
} catch (ImagickException $e) {
    die('ImagickException ' . $e->getMessage());
} catch (Exception $e) {
    die('Exception ' . $e->getMessage());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文