在 pdf 缩略图创建中显示自定义错误消息 - PHP - Imagick
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在理论中,您可以使用
identify
命令的-format
参数的%n
变量来查找运行主命令之前给定文件(包括 PDF)的页数(“帧”、“图像”)。这样您就可以使用结果将正确的值填充到主命令中,这样您就不会首先收到错误消息:在实践中,这可以是非常慢(因为 ImageMagick 似乎首先渲染完整的 PDF 以计算页数)。
因此,我的建议是使用外部命令行工具来获取 PDF 页数:
pdfinfo
。该工具专门用于 PDF 文件,它知道在哪里查找该信息(因为该数字是任何 PDF 文件所需元数据的一部分,pdfinfo 不需要先渲染每个页面,然后才能吐出该信息)信息):为了让您看到这两个命令的不同性能级别,我确实针对包含 PDF-1.7 的官方 ISO 32000 规范的文件运行了它:
因此 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 theidentify
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: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):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:
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...'