在 PHP (LAMP) 中创建文档(PDF、DOC、XLS 等)的缩略图预览

2021-12-30 00:00:00 graphics linux php lamp

当用户将某些文件(例如 .doc、.xls、.pdf 等)上传到我的网站时,我希望能够生成(文档第一页的)预览缩略图.我正在 LAMP 堆栈中使用 PHP,但对可以完成这项工作的任何库或命令行工具感到满意(Linux 非常受欢迎).

When users upload certain files to my site (such as .doc, .xls, .pdf, etc) I'd like to be able to generate a preview thumbnail (of the first page of the document). I'm working with PHP in a LAMP stack but would be happy with any library or command-line tool that can do the job (Linux highly preferred).

推荐答案

将某些文档格式转换为图像并不容易.单独的 php 无法做到这一点.执行此操作的正确"方法是首先在您的服务器上安装可以以该格式打开文档的程序.例如,对于 .doc 文档,您可以使用 OpenOffice它还可以打开大多数其他文档格式然后你需要设置你的开放式办公室以无头"模式工作,将输出发送到虚拟显示器(XVFB 是你在 Linux 上需要的)

It's not easy to convert certain document formats to image. php alone cannot do this. The 'proper' way to do this is to first of all have the program installed on your server that can open the document in that format. For example, for .doc documents you can use OpenOffice it also can open most other document formats You then need to setup your open office to work in 'headless' mode, sending the output to virtual display (XVFB is what you going to need on Linux)

然后您的 php 脚本将调用 OpenOffice,将路径传递给上传的文档.OpenOffice 将实际打开该文档.然后你需要从屏幕缓冲区创建一个图像.您可以为此使用 ImageMagick

You php script will then call OpenOffice, passing the path to uploaded doc. OpenOffice will actually open that doc. Then you need to create an image from the screen buffer. You can use ImageMagick for that

然后,一旦您捕获了屏幕,您就可以将其调整为缩略图.

Then once you have the capture of your screen you can resize it to a thumbnail.

查看此链接了解更多详情

Look at this link for more details

http://www.mysql-apache-php.com/website_screenshot.htm

相关文章