组件下载: http://wkhtmltopdf.org/
Ubuntu中文支持: apt-get install openssl build-essential xorg libssl-dev libxrender-dev fonts-arphic-bsmi00lp fonts-arphic-gbsn00lp xfonts-intl-chinese
测试命令:./wkhtmltoimage baidu.com baidu.png
php中使用:
// api.php
<?php
$url = isset($_GET['url']) ? $_GET['url'] : die('url param is require');
$cache = __DIR__ .'/cache/' . md5($url) . '.png';
if (!is_file($cache)) {
// 这里写组件的存放位置, 主要要给文件可执行权限 chmod 777 wkhtmltoimage
$shell = __DIR__ . '/shell/wkhtmltox/bin/wkhtmltoimage '. $url .' '. $cache;
shell_exec($shell);
}
header("Content-Type: image/png");
$im = imagecreatefrompng($cache);
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
然后访问 http://demo.zhaishuaigan.cn/php/html2img/api.php?url=baidu.com