screenshot_krkrnews

安裝 wkhtmltoimage

我的 server 是 Ubuntu 14.04,安裝如下

sudo add-apt-repository ppa:ecometrica/servers
sudo apt-get update
sudo apt-get install wkhtmltopdf  

Command line 模式使用

wkhtmltoimage http://www.google.com.tw screenshot.png

個人認為比較重要的參數有
–crop-h 圖片裁切的高度
–crop-w 圖片裁切的寬度
–crop-x 圖片裁切的 X 座標
–crop-y 圖片裁切的 Y 座標
–quality 圖片輸出的品質 (0 ~ 100, 預設值為 94)
其他詳細參數可以參考這裡

在 PHP 上如何使用

你可以直接透過 exec 執行命令列(Command line)操作。也可以使用 snappy 進行操作。這是一個 GitHub 上其他高手寫好的 wrapper 。

專案名稱: snappy
來源網址: https://github.com/KnpLabs/snappy

在 Ruby 上如何使用

你可以使用這個在 Ruby 上的 wrapper

專案名稱: IMGKit
來源網址: https://github.com/csquared/IMGKit

最後,如果遇到中文亂碼怎麼辦?

記得在 ubuntu 上安裝中文字型。這裏我之前是參考霹靂開發幫。

apt-get install libxrender-dev ttf-arphic-bsmi00lp ttf-arphic-gbsn00lp xfonts-intl-chinese

32位系统安装方案:

# cd /tmp
# chown www:www ./wkhtmltoimage-i386
# chmod +x ./wkhtmltoimage-i386
# mv ./wkhtmltoimage-i386 /usr/bin/wkhtmltoimage-i386
# cd ~


注释:googlecode下载需要代理翻墙才可以
wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2的版本会有问题



php使用方法:
 
// Name of your output image
$name = "example.jpg";
 
// Command to execute
$command = "/usr/bin/wkhtmltoimage-i386 --width 300 --height 200 --quality 60 --zoom 0.3 --load-error-handling ignore";
 
// Directory for the image to be saved,目录需要有写权限
$image_dir = "/var/www/images/";
 
// Putting together the command for `shell_exec()`
$ex = "$command $url " . $image_dir . $name;
 
// The full command is: "/usr/bin/wkhtmltoimage-i386 --load-error-handling ignore http://www.google.com/ /var/www/images/example.jpg"
// If we were to run this command via SSH, it would take a picture of google.com, and save it to /vaw/www/images/example.jpg
 
// Generate the image
// NOTE: Don't forget to `escapeshellarg()` any user input!
$output = shell_exec($ex);

喜欢这篇文章?

点击后弹出微信二维码,可自选金额或输入其他金额