本文介绍了imagick的PHP只显示最后一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将imagick与PHP/Windows IIS一起使用.我有一个简单的脚本,可将TIF文件转换为PDF,并将其呈现给浏览器.它可以完美地处理单页TIF文件,但可以处理多页,仅显示最后一页.

I am using imagick with PHP/Windows IIS. I have a simple script that converts a TIF file to PDF an presents it to the browser. It works flawlessly with single page TIF files, but with multiple pages, its only showing the last page.

我知道它默认显示最后一页,因为$ im变量是一个数组.我试图对其进行的任何尝试都将使其无效的PDF.下面是我的代码.我是imagick的新手,感谢您的帮助!

I understand that it shows the last page by default because the $im variable is an array. Any attempt I make to fix it makes it an invalid PDF. Below is my code. I am new to imagick and any help is appreciated!

$im = new imagick("tmp/tmp.tif");
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;

ImageMagick版本ImageMagick 7.0.7-11 Q16 x64 2017-11-23

ImageMagick version ImageMagick 7.0.7-11 Q16 x64 2017-11-23

ImageMagick库版本ImageMagick 7.0.7-11 Q16 x64 2017-11-23

ImageMagick library version ImageMagick 7.0.7-11 Q16 x64 2017-11-23

(这是非常粗糙的测试代码,我待会儿清理)

(this is very rough testing code, I will clean it up later)

推荐答案

内部图像迭代器指向最后读取的页面.您只需使用 Imagick::setFirstIterator 将其重置为首页. /p>

The internal image iterator is pointing at the last page read. You just need to reset it to the first page with Imagick::setFirstIterator.

$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im->getImage();

甚至

$im->setIteratorIndex(0);

根据评论进行编辑

如果您尝试输出整个PDF文档,则可以使用 Imagick::getImagesBlob .

If you are attempt to output the entire PDF document, you would use Imagick::getImagesBlob.

$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
$blob = $im->getImagesBlob();
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($blob));
echo $blob;

这篇关于imagick的PHP只显示最后一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:11