在 PHP 中将 HTML 转换为 PNG 或 JPG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10228832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:42:41  来源:igfitidea点击:

Convert HTML to PNG or JPG in PHP

php

提问by Chris van der Berg

Possible Duplicate:
rendering html to png (server-side)

可能的重复:
将 html 渲染为 png(服务器端)

I have a DIV element with some images positioned within the DIV element. I want to make a JPG or a PNG file out of that, so people can save it for their Facebook timeline cover image. I found a LOT of scripts and tutorials, but nothing really fills my needs. This can't really be a very hard thing to do?

我有一个 DIV 元素,其中一些图像位于 DIV 元素内。我想从中制作 JPG 或 PNG 文件,以便人们可以将其保存为 Facebook 时间线封面图片。我找到了很多脚本和教程,但没有什么能真正满足我的需求。这真的不是一件很难的事情吗?

I already got my host to install iMagick.

我已经让我的主机安装 iMagick。

采纳答案by dislick

You should get the positions via JavaScript (I guess you are using js to set the positions of the images in the div) and send them to a PHP-Script. With help of the PHP GD manualyou can easily generate a png oder jpeg image.

您应该通过 JavaScript 获取位置(我猜您正在使用 js 来设置 div 中图像的位置)并将它们发送到 PHP 脚本。借助PHP GD 手册,您可以轻松生成 png 或 jpeg 图像。

回答by Jürgen Paul

You can try

你可以试试

WKHTMLTOIMAGE

WKHTMLTOIMAGE

回答by Kenny

I had to achieve the same thing, only for something else than Facebook and I used webkit2png

我必须达到同样的目的,只是为了 Facebook 以外的其他东西,我使用了webkit2png

I save the HTML code on a temporary HTML file, then I run the python webkit2png command on that temporary HTML file which converts it to PNG. It requires xvfb (apt-get install xvfb)

我将 HTML 代码保存在临时 HTML 文件中,然后在该临时 HTML 文件上运行 python webkit2png 命令,将其转换为 PNG。它需要 xvfb (apt-get install xvfb)

I then use the following command in PHP:

然后我在 PHP 中使用以下命令:

exec('xvfb-run --server-args="-screen 0, 1024x768x24" python webkit2png.py --log=/tmp/webkit2pnglog.log --output=' .$fullUploadPath . ' ' . $fullPath);

Where $fullUploadPath is the location of the target file and $fullPath is the location of the temporary HTML file.

其中 $fullUploadPath 是目标文件的位置,$fullPath 是临时 HTML 文件的位置。