我正在与PHP ImageMagick Library合作,将上传的pdf文件转换为单一的png文件,这样我就可以在我的Laravel网页上将pdf显示为单一的图像.到目前为止,我可以使用以下代码将整个pdf转换为单个图像:
<?php
$imagick = new Imagick();
$file = new File;
// other lines of code
// ...
$imgPath = Storage::path($file->file_path);
$imgSavePath = Storage::path('uploads/buffer/'.Str::beforeLast($file->name, '.').'.png');
$imagick->readImage($imgPath);
$imagick->resetIterator();
$imagick = $imagick->appendImages(true);
$imagick->writeImages($imgSavePath, true);
这是通过生成单个PNG图像实现的.然而,我发现这是资源密集型的(存储方面的)和耗时的,因为我是通过一个AJAX调用来交付功能的.
我希望我的网络应用程序转换只有前n页的pdf(说前5页)为一个单一的图像,作为网站上的预览-此后,用户可以下载整个pdf查看他们的本地系统.无论上传的pdf文档的页数是多少,该功能都应该有效.
到目前为止,我只在documentation中发现可以从Imagick对象读取特定索引处的页面并使用以下命令转换为图像:
...
$imgPath = Storage::path($file->file_path);
$index = 5;
$imagick->readImage($imgPath. '[' . $index . ']');
然而,我发现很难重构它,这样应用程序就可以读取前n页.