真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

php如何調(diào)用phantomJS截圖

推薦:《PHP視頻教程》

十余年的灌云網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整灌云建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“灌云網(wǎng)站設(shè)計”,“灌云網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

php調(diào)用phantomJS截圖

知識儲備

*unix系統(tǒng)安裝phantomjs,權(quán)限相關(guān)知識

基本JavaScript語法知識

php exec函數(shù)調(diào)用REPL phantomjs

phantomjs js截圖文檔 http://javascript.ruanyifeng.com/tool/phantomjs.html

代碼(php 代碼環(huán)境為yii2框架)

js_path = "{$dir}/script.js";
        /** @var bool|string 獲取php 有777權(quán)限的臨時文件目錄 */
        $this->temp_dir = \\Yii::getAlias('@runtime');
    }
    /**
     * 截圖并上傳
     * @param string $url
     * @param string $filename
     * @return string
     * @throws BizException
     */
    public function screenShotThenSaveToOss(string $url, string $filename = 'temp.jpg')
    {
        //輸出圖片的路徑
        $outputFilePath = "{$this->temp_dir}/$filename";
        //執(zhí)行的phantomjs命令
        //phantomjs 可執(zhí)行文件必須是 絕對路徑 否則導(dǎo)致 exec 函數(shù)返回值127錯誤
        $cmd = "\\usr\\local\\bin\\phantomjs {$this->js_path} '$url' '$outputFilePath'";
        //捕捉不到phantomjs命令輸出結(jié)果
        exec($cmd, $output);
        //檢查截圖文件是否存在
        $isShotImgaeExist = file_exists($outputFilePath);
        if (!$isShotImgaeExist) {
            throw new BizException(0, 'phantomjs截圖失敗', BizException::SELF_DEFINE);
        }
        //保存截圖到oss
        $result = $this->postScreenShotImageToOss($outputFilePath);
        //刪除臨時文件夾的截圖圖片
        unlink($outputFilePath);
        return $result;
    }
    /**
     * 上傳截圖到阿里云直傳oss
     * @param string $screenshot_path
     * @return string
     */
    public function postScreenShotImageToOss(string $screenshot_path): string
    {
        $ossKey = 'raw_file_name';
        $file = new \\CURLFile($screenshot_path, 'image/jpeg', 'file');
        $tokenArray = $this->getOssPolicyToken('fetch');
        $url = $tokenArray->host;
        $postData = [
            'key' => "{$tokenArray->dir}/$ossKey",
            'policy' => $tokenArray->policy,
            'OSSAccessKeyId' => $tokenArray->accessid,
            'success_action_status' => '200',
            'signature' => $tokenArray->signature,
            'callback' => $tokenArray->callback,
            'file' => $file
        ];
        $ch = curl_init();
        //$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
        curl_setopt($ch, CURLOPT_URL, $url);
        // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // required as of PHP 5.6.0
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: $mime_type"]);
        $res = curl_exec($ch);
        $res = json_decode($res);
        curl_close($ch);
        if (empty($res) || $res->code != 0) {
            return '';
        } else {
            return $res->data->url;
        }
    }
    /**
     * 調(diào)用管理后臺阿里云oss token接口
     * @param null $url
     * @return array
     */
    public function getOssPolicyToken($url = null)
    {
        $url = \\Yii::$app->params['oss_screen_shot_token_api'];
        $ch = curl_init();
        // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // Will return the response, if false it print the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Set the url
        curl_setopt($ch, CURLOPT_URL, $url);
        // Execute
        $result = curl_exec($ch);
        // Closing
        curl_close($ch);
        $res = json_decode($result);
        if (empty($res) || $res->code != 0) {
            return [];
        } else {
            return $res->data;
        }
    }
}
phantomjs javascript腳本內(nèi)容
"use strict";
var system = require('system');
var webPage = require('webpage');
var page = webPage.create();
//設(shè)置phantomjs的瀏覽器user-agent
page.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1';
//獲取php exec 函數(shù)的命令行參數(shù)
if (system.args.length !== 3) {
    console.log(system.args);
    console.log('參數(shù)錯誤');
    console.log('第2個參數(shù)為url地址 第3個參數(shù)為截圖文件名稱');
    phantom.exit(1);
}
//命令行 截圖網(wǎng)址參數(shù)
var url = system.args[1];
//圖片輸出路徑
var filePath = system.args[2];
console.log('-------');
console.log(url);
console.log('-------');
console.log(filePath);
console.log('-------');
//設(shè)置瀏覽器視口
page.viewportSize = {width: 480, height: 960};
//打開網(wǎng)址
page.open(url, function start(status) {
    //1000ms之后開始截圖
    setTimeout(function () {
        //截圖格式為jpg 80%的圖片質(zhì)量
        page.render(filePath, {format: 'jpg', quality: '80'});
        console.log('success');
        //退出phantomjs 避免phantomjs導(dǎo)致內(nèi)存泄露
        phantom.exit();
    }, 1000);
});

當(dāng)前名稱:php如何調(diào)用phantomJS截圖
轉(zhuǎn)載注明:http://weahome.cn/article/cpiphj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部