组件下载: 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中使用:

  1. // api.php
  2. <?php
  3. $url = isset($_GET['url']) ? $_GET['url'] : die('url param is require');
  4. $cache = __DIR__ .'/cache/' . md5($url) . '.png';
  5. if (!is_file($cache)) {
  6. // 这里写组件的存放位置, 主要要给文件可执行权限 chmod 777 wkhtmltoimage
  7. $shell = __DIR__ . '/shell/wkhtmltox/bin/wkhtmltoimage '. $url .' '. $cache;
  8. shell_exec($shell);
  9. }
  10. header("Content-Type: image/png");
  11. $im = imagecreatefrompng($cache);
  12. imagesavealpha($im, true);
  13. imagepng($im);
  14. imagedestroy($im);

然后访问 http://demo.zhaishuaigan.cn/php/html2img/api.php?url=baidu.com