Home >php教程 >php手册 >LINUX下PHP网页生成快照(截屏)(xvfb and wkhtmltoimage)

LINUX下PHP网页生成快照(截屏)(xvfb and wkhtmltoimage)

WBOY
WBOYOriginal
2016-06-13 09:49:121583browse

利用php截屏或实现网页快照我们需要用一个工具:xvfb and wkhtmltoimagek哦,这个工具目前只能在linux系统中使用,下面有兴趣的朋友可进入参考。

在做旅游攻略时,需要把攻略页面生成PDF和Jpg图片,PDF生成用的是《MPDF把Html生成PDF》,而HTML生成Jpg又卡壳了。前期打算以纯程式的生成,但是除了调用别的网站提供的远程API生成Jpg,不然以单纯的程式(GD库和内置函数,或封装的类)都没找到完美的解决。

不得已,安装扩展wkhtmltoimage生成。

安装步骤如下:

1、服务器端下载安装wkhtmltoimage,32和64位有区别,心情天空安装的是centos 5.8 32位。但是下载了 wkhtmltoimage 0.11版本但是出现以下错误,

Pixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used
QPixmap: Cannot create a QPixmap when no GUI is being used最后心情天空安装wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2低版本才可以正常生成。


32位
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2
tar jxf wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2
cp wkhtmltoimage-i386 /usr/local/bin/wkhtmltoimage
 
64位
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2
mv wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2 wkhtmltoimage-0.10.0_rc2-static-amd64.tar
tar -xvf wkhtmltoimage-0.10.0_rc2-static-amd64.tar
mv wkhtmltoimage-amd64 /usr/bin/wkhtmltoimage
就这样,安装成功,可以测试 wkhtmltoimage http://www.bKjia.c0m/ screenshot.png

2、php执行wkhtmltoimage 生成图片

 代码如下 复制代码

set_time_limit(0);
shell_exec('/usr/local/bin/wkhtmltoimage http://www.bKjia.c0m/ /usr/local/wwwroot/bKjia.c0m/5eyi.jpg');

假若用foreach批量生成的话建议,sleep(10),每次生成后停顿几秒。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn