CutyCapt 基於 Qt WebKit,能無頭渲染網頁並輸出圖片,是 PHP 實現網頁截圖的經典方案。本文詳述 Windows 直接調用與 Linux 編譯安裝全流程,含中文、Flash 支援。
官網:sourceforge.net/projects/cutycapt,下載 CutyCapt.exe 放入項目目錄(如 D:/webserver/)。
"; system($cmd); // 或 exec($cmd, $output, $return_var);
參數說明:--url 目標網址,--out 輸出路徑,支援 png/jpg/pdf/svg。
# 添加 ATrpms 源 cat > /etc/yum.repos.d/atrpms.repo <<'EOF' [atrpms] name=CentOS $releasever - $basearch - ATrpms baseurl=https://dl.atrpms.net/el$releasever-$basearch/atrpms/stable gpgkey=https://ATrpms.net/RPM-GPG-KEY.atrpms gpgcheck=1 enabled=1 EOF yum update -y yum install -y qt47 qt47-devel qt47-webkit qt47-webkit-devel # 若報 libGLU.so.1 / libmng.so.1 缺失: yum install -y mesa-libGLU libmng
cd /data0/software svn co https://cutycapt.svn.sourceforge.net/svnroot/cutycapt mv cutycapt/CutyCapt /usr/local/cutycapt cd /usr/local/cutycapt qmake make # 若報錯:yum update sqlite && make # 仍失敗:qmake-qt47 && make
wget https://www.flexthinker.com/wp-content/uploads/2009/11/xvfb-run.sh.txt mv xvfb-run.sh.txt /usr/local/cutycapt/xvfb-run.sh chmod +x /usr/local/cutycapt/xvfb-run.sh
yum install -y fonts-chinese ImageMagick
/usr/local/cutycapt/xvfb-run.sh --server-args="-screen 0, 1024x768x24" \ /usr/local/cutycapt/CutyCapt --url=https://www.jb51.net --out=/tmp/test.jpg
# 下載 64 位 Flash Player cd /data0/software wget -O libflashplayer.so "https://ishare.iask.sina.com.cn/f/13659493.html" cp libflashplayer.so /usr/lib64/flash-plugin/ # 驗證鏈接 ls -l /usr/lib64/mozilla/plugins/libflashplayer.so
class WebCapture {
private $cutycaptPath;
private $xvfbRunPath;
private $isWindows;
public function __construct() {
$this->isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
if ($this->isWindows) {
$this->cutycaptPath = 'D:/webserver/CutyCapt.exe';
} else {
$this->cutycaptPath = '/usr/local/cutycapt/CutyCapt';
$this->xvfbRunPath = '/usr/local/cutycapt/xvfb-run.sh';
}
}
public function capture($url, $outputPath, $width = 1024, $height = 768) {
$cmd = $this->isWindows
? "\"{$this->cutycaptPath}\" --url={$url} --out={$outputPath} --min-width={$width} --min-height={$height}"
: "{$this->xvfbRunPath} --server-args=\"-screen 0, {$width}x{$height}x24\" {$this->cutycaptPath} --url={$url} --out={$outputPath} --min-width={$width} --min-height={$height}";
exec($cmd, $output, $returnVar);
return $returnVar === 0 && file_exists($outputPath);
}
// 後處理:裁剪、縮放
public function postProcess($inputPath, $outputPath, $cropW, $cropH, $resizePercent = null) {
$cmd = "convert -crop {$cropW}x{$cropH}+0+0 {$inputPath} {$outputPath}";
if ($resizePercent) {
$cmd .= " && convert -resize {$resizePercent}% {$outputPath} {$outputPath}";
}
exec($cmd, $out, $ret);
return $ret === 0;
}
}
// 使用示例
$capture = new WebCapture();
$capture->capture('https://www.jb51.net', '/tmp/jb51.png');
$capture->postProcess('/tmp/jb51.png', '/tmp/jb51_thumb.png', 1024, 768, '15%');
| 問題 | 原因 | 解決 |
|---|---|---|
| 截圖空白/只有一半 | 頁面未加載完成就截圖 | 增加 --delay=5000 延遲 5 秒,或使用 --javascript=on(預設開啟) |
| 中文顯示方框 | 缺中文字體 | 安裝 fonts-chinese 或 wqy-zenhei |
| Flash 區域空白 | 無 Flash 插件 | 安裝 64 位 libflashplayer.so 到 /usr/lib64/flash-plugin/ |
| Linux 下報錯 "cannot connect to X server" | 未使用 Xvfb | 必須通過 xvfb-run.sh 啟動 |
| PHP exec 無權限 | disable_functions 限制 / selinux | php.ini 移除 exec,設置 selinux 允許 httpd 執行 |
| 方案 | 優點 | 缺點 | 推薦場景 |
|---|---|---|---|
| CutyCapt (Qt WebKit) | 成熟穩定、支援 Flash | WebKit 老舊、CSS3/ES6 支援差、維護停止 | 舊系統維護、需 Flash 的場景 |
| Puppeteer (Headless Chrome) | 現代渲染引擎、完整 DevTools 協議、積極維護 | 需 Node.js 環境、內存佔用較大 | 新項目首選、複雜 SPA 頁面 |
| wkhtmltoimage (Qt WebKit) | 輕量、命令行友好 | 同 CutyCapt,引擎老舊 | 簡單靜態頁轉圖片/PDF |
| Chrome Headless + PHP (chrome-php/chrome) | 純 PHP 調用 Chrome、現代引擎 | 需安裝 Chrome、相對較新 | 不想引入 Node.js 的 PHP 專案 |