麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

php調用phantomjs給微信小程序分享

2020-03-22 19:52:28
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了關于php調用phantomjs給html' target='_blank'>微信小程序分享,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

知識儲備

*unix系統安裝phantomjs,權限相關知識

基本JavaScript語法知識

php exec函數調用REPL phantomjs

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

代碼(php 代碼環境為yii2框架)
 ?phpnamespace weapp/library/phantomjs;use weapp/library/BizException;class ScreenShot /** @var string 獲取phantomjs 參數中 js文件的決定路徑 */ private $js_path; /** @var bool|string 獲取php 有777權限的臨時文件目錄 */ private $temp_dir; function __construct() $dir = __DIR__; $this- js_path = {$dir}/script.js  /** @var bool|string 獲取php 有777權限的臨時文件目錄 */ $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  //執行的phantomjs命令 //phantomjs 可執行文件必須是 絕對路徑 否則導致 exec 函數返回值127錯誤 $cmd = /usr/local/bin/phantomjs {$this- js_path} $url $outputFilePath  //捕捉不到phantomjs命令輸出結果 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;
$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- * 調用管理后臺阿里云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腳本內容
 use strict var system = require( system var webPage = require( webpage var page = webPage.create();//設置phantomjs的瀏覽器user-agentpage.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 函數的命令行參數if (system.args.length !== 3) { console.log(system.args); console.log( 參數錯誤  console.log( 第2個參數為url地址 第3個參數為截圖文件名稱  phantom.exit(1);//命令行 截圖網址參數var url = system.args[1];//圖片輸出路徑var filePath = system.args[2];console.log( ------- console.log(url);console.log( ------- console.log(filePath);console.log( ------- //設置瀏覽器視口page.viewportSize = {width: 480, height: 960};//打開網址page.open(url, function start(status) { //1000ms之后開始截圖 setTimeout(function () { //截圖格式為jpg 80%的圖片質量 page.render(filePath, {format: jpg , quality: 80  console.log( success  //退出phantomjs 避免phantomjs導致內存泄露 phantom.exit(); }, 1000);});
php調用phantomjs目錄結構

3184207835-5b28e6f5859bb_articlex[1].png

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !

相關推薦:

Laravel微信小程序獲取用戶詳細信息及帶參數小程序碼擴展的分析

gitbash PHP執行輸出中文亂編解決方式

php的快速排序的代碼

以上就是php調用phantomjs給微信小程序分享的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕免费在线观看视频 | 色戒在线版 | 久久久看 | 91精品国产一区二区三区动漫 | 高清在线观看av | 国产精品久久久久久久久久久久午夜 | 毛片免费看网站 | 免费香蕉成视频成人网 | 在线亚洲免费视频 | 日韩毛片免费观看 | 国产在线久 | 国产永久免费观看 | 福利在线国产 | 撅高 自己扒开 调教 | 国色天香综合网 | 亚洲国产小视频 | 亚洲爱爱图 | h视频免费观看 | asian超清日本肉体pics | 国产精品美女一区二区 | 一级α片免费看刺激高潮视频 | 欧美亚洲国产一区 | 久久国产亚洲视频 | 欧美日韩在线视频一区二区 | 日本在线视| 91精选视频在线观看 | 黑人一区| 日韩视频一区二区在线观看 | 91成人亚洲 | 天天看夜夜爽 | 久久人人97超碰国产公开结果 | 久久无 | 精品黑人一区二区三区国语馆 | 精品亚洲综合 | 免费看毛片的网站 | 天堂成人一区二区三区 | 亚洲国产网址 | 狠狠干91| 午夜精品在线视频 | www.99久| 久久男人天堂 |