首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 使用 CutyCapt 實現網頁截圖完整教程(Windows/Linux 雙平台)

     編輯:PHP 綜合知識

前言

CutyCapt 基於 Qt WebKit,能無頭渲染網頁並輸出圖片,是 PHP 實現網頁截圖的經典方案。本文詳述 Windows 直接調用與 Linux 編譯安裝全流程,含中文、Flash 支援。

Windows 平台:直接調用 .exe

1. 下載 CutyCapt

官網:sourceforge.net/projects/cutycapt,下載 CutyCapt.exe 放入項目目錄(如 D:/webserver/)。

2. PHP 調用代碼

";
system($cmd); // 或 exec($cmd, $output, $return_var);

參數說明:--url 目標網址,--out 輸出路徑,支援 png/jpg/pdf/svg。

Linux 平台:編譯安裝全流程(CentOS 7 示例)

1. 安裝 Qt 4.7+ 依賴

# 添加 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

2. 編譯 CutyCapt

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

3. 配置虛擬顯示器 Xvfb

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

4. 安裝中文字體與 ImageMagick

yum install -y fonts-chinese ImageMagick

5. 測試截圖

/usr/local/cutycapt/xvfb-run.sh --server-args="-screen 0, 1024x768x24" \
/usr/local/cutycapt/CutyCapt --url=https://www.jb51.net --out=/tmp/test.jpg

6. 解決 Flash 內容無法渲染

# 下載 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

PHP 統一調用封裝(跨平台)

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 限制 / selinuxphp.ini 移除 exec,設置 selinux 允許 httpd 執行

現代替代方案對比

結語

CutyCapt 曾是 PHP 網頁截圖主流方案,但隨著 WebKit 停止更新,新項目強烈建議遷移至 Puppeteer 或 Chrome Headless。若必須維護舊系統,本文提供的雙平台部署指南可助你快速落地。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
方案優點缺點推薦場景
CutyCapt (Qt WebKit)成熟穩定、支援 FlashWebKit 老舊、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 專案