Imagick PHP only displaying last page

怎甘沉沦 提交于 2019-12-11 17:08:29

问题


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.

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 version 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)


回答1:


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();

Or even

$im->setIteratorIndex(0);

Edit based on comments

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;


来源:https://stackoverflow.com/questions/54539023/imagick-php-only-displaying-last-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!