yum install epel-release
yum install xorg-x11-fonts*
yum install qtwebkit-devel qt-devel
yum -y install CutyCapt #安装CutyCapt
yum -y install Xvfb #没有图形界面所以需要安装这个
yum -y install ImageMagick
安装完成之后写个脚本测试一下,脚本代码如下,脚本文件命名为 html2img.sh
xvfb-run \
--server-args="-screen 0, 640x480x24" \
CutyCapt \
--url="$1" \
--body-string="utf-8" \
--delay=3000 \
--out=$2 \
测试代码
sh html2img.sh "https://www.baidu.com" baidu.jpg
执行完成之后可以看到有生成baidu.jpg,但其中的中文是乱码,需要安装中文字体才能正常显示,安装步骤如下
fc-list #查看当前已经安装的字体,应该是没有安装中文字体的,才会出现乱码
yum -y install fontconfig #安装字体配置处理
mkdir /usr/share/fonts/chinese/ #创建目录用于存放中文字体
找一台windows机器进入C:\Windows\Fonts目录找到simsun.ttf (宋体)、simheittf.ttf (黑体)
然后上传到centos7机器的/usr/share/fonts/chinese/目录下
chmod -R 755 /usr/share/fonts/chinese #修改权限
yum -y install ttmkfdir
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
vim /etc/fonts/fonts.conf 在文件中找到 <!-- Font directory list --> 模块,
并将 <dir>/usr/share/fonts/chinese</dir> 添加进去
fc-cache #刷新字体
fc-list #再次查看当前已经安装的字体,应该能看到多出来新装的两种中文字体了
再次执行测试
sh html2img.sh "https://www.baidu.com" baidu.jpg
打开生成的图片应该能看到中文字符可以正常显示了。
参考文章:
Linux CentOS 7 安装字体库 & 中文字体
Linux CentOS 7 安装 字体库文件(simsun.ttf、simheittf.ttf)
php使用CutyCapt实现网页截图保存的方法