Skip to content

Instantly share code, notes, and snippets.

@kinokoRider
Last active November 7, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinokoRider/06dec55fec18301565d93105fdcacc10 to your computer and use it in GitHub Desktop.
Save kinokoRider/06dec55fec18301565d93105fdcacc10 to your computer and use it in GitHub Desktop.
ScreenShot.php
<?php
//yahooのトップをブラウザの横幅を1000として撮影し、
//screenshot.jpgというファイルに保存する。
$screenshot = new ScreenShot(1000, 750, 'https://www.yahoo.co.jp/');
$screenshot->MakeImg('screenshot.jpg');
?>
<?php
//phantomjsを使って特定のWebサイトのスクショを取って保存するクラスです。
//jonnnnyw/php-phantomjs のラッパー?ラッピング?したものです。
//参考:https://github.com/jonnnnyw/php-phantomjs
use \JonnyW\PhantomJs\Client;
class ScreenShot{
private $client;
private $request;
private $response;
private $width;
private $height;
private $top;
private $left;
private $URL;
private $timeOut = 30000;
//画面サイズなどを決定する
function __construct($width, $height, $URL){
$this->width = $width;
$this->height = $height;
$this->top = 0;
$this->left = 0;
$this->URL = $URL;
}
//phantomjs実行クライアントを作成する
public function MakeClient(){
$this->client = Client::getInstance();
$this->client->isLazy(); //ページのレンダリング待ち
$this->client->getEngine()->setPath(__DIR__ . '/../bin/phantomjs');
}
//スクショリクエストの詳細を設定する
//ここでは、ブラウザサイズと同じ範囲を撮影範囲としている
public function MakeRequest($screenshotFileName){
$this->request = $this->client->getMessageFactory()->createCaptureRequest($this->URL, 'GET');
$this->request->setTimeout($this->timeOut); //強制的に終了するまでの時間
$this->request->setOutputFile($screenshotFileName); //スクショのファイル名
$this->request->setViewportSize($this->width, $this->height); //ブラウザサイズ
$this->request->setCaptureDimensions($this->width, $this->height, $this->top, $this->left); //撮影範囲
}
//リクエストのレスポンス(の受け皿)を作成する
public function MakeRespose(){
$this->response = $this->client->getMessageFactory()->createResponse();
}
//リクエストを送信する
public function SendRequset(){
$this->client->send($this->request, $this->response);
}
//
public function MakeImg($screenshotFileName){
$this->MakeClient();
$this->MakeRequest($screenshotFileName);
$this->MakeRespose();
$this->SendRequset();
//ファイルサイズ 0 byte = 画面取得できてないと判断
If(filesize($ssName) != 0){
return true;
}else{
return false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment