PhantomJS를 사용하여 다양한 화면 캡처를 시도했습니다.
며칠 전 우연히 프로젝트 담당 테스트(Capybara)에 오류가 발생한 것을 발견하고 반해 팬텀JS 주변을 조사했다.
PhantoomJS란?
PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
Webkit 기반Headless 브라우저는 DOM 처리, CSS 선택기, JSON,Canvas, SVG를 지원합니다.
PhantomJS 설치
코드 라인 (mac)
맥에서 brew를 사용하여phantomjs를 가져옵니다.brew install phantomjs
결과 내보내기NPC0201:~ livesense$ sudo brew install phantomjs
==> Downloading https://homebrew.bintray.com/bottles/phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
Already downloaded: /Users/livesense/Library/Caches/Homebrew/phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
==> Pouring phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
🍺 /usr/local/Cellar/phantomjs/2.1.1: 49 files, 51.0M
composer
이번에phantomjs php의 프로그램 라이브러리를 사용하기 때문에compooser를 사용하여 패키지를 관리합니다.
composer.json의 내용
composer.json{
"scripts": {
"post-install-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
],
"post-update-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
]
},
"require": {
"jonnyw/php-phantomjs": "4.*"
}
}
설치 결과NPC0201:sample livesense$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing twig/twig (v1.28.2)
Downloading: 100%
- Installing symfony/yaml (v3.2.0)
Downloading: 100%
- Installing symfony/filesystem (v3.2.0)
Downloading: 100%
- Installing symfony/dependency-injection (v3.2.0)
Downloading: 100%
- Installing symfony/config (v3.2.0)
Downloading: 100%
- Installing jakoch/phantomjs-installer (2.1.1)
Downloading: 100%
- Installing jonnyw/php-phantomjs (v4.5.1)
Downloading: 100%
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
symfony/dependency-injection suggests installing symfony/expression-language (For using expressions in service container configuration)
symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
Writing lock file
Generating autoload files
- Installing phantomjs (2.1.1)
Downloading: 100%
화면 캡처 코드(API)
PhantomJS로 화면을 간단하게 찍을 수 있어서 그 API를 만들어 봤어요.
index.php<?php
require __DIR__ . '/../vendor/autoload.php';
use JonnyW\PhantomJs\Client;
$web_url = $_GET['web_url'];
$output_file = '/data/' . sha1($_SERVER['REQUEST_URI']) . '.jpg';
if (file_exists($output_file)) {
header('Content-type: image/jpeg');
readfile($output_file);
} else {
// ファイルを分割してserviceクラスなどにまとめたほうがよい
$viewport_width = 1600;
$viewport_height = 1000;
$client = Client::getInstance();
$client->getEngine()->setPath('/usr/bin/phantomjs'); // phantomjs path
$request = $client->getMessageFactory()->createCaptureRequest(
$web_url, // url
'GET', // method
3000); // timeout(second)
$client->getEngine()->addOption('--proxy=host:port');
$request->setQuality(75); // default 75
$request->setCaptureDimensions($width, $height); // キャプチャ範囲
$request->setFormat('jpeg'); // pdf, png, jpeg, bmp, ppm, gif
$request->setOutputFile($output_file);
$request->setViewportSize($viewport_width, $viewport_height);
$response = $client->getMessageFactory()->createResponse();
$client->send($request, $response);
header('Content-type: image/jpeg');
readfile($output_file);
}
다음은 IESHIL 첫 페이지의 캡처입니다.
http://localhost/?web_url=https%3A//www.ieshil.com/
(왠지 영상이 잘 안 나온다)
참고 자료
코드 라인 (mac)
맥에서 brew를 사용하여phantomjs를 가져옵니다.
brew install phantomjs
결과 내보내기NPC0201:~ livesense$ sudo brew install phantomjs
==> Downloading https://homebrew.bintray.com/bottles/phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
Already downloaded: /Users/livesense/Library/Caches/Homebrew/phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
==> Pouring phantomjs-2.1.1.el_capitan.bottle.1.tar.gz
🍺 /usr/local/Cellar/phantomjs/2.1.1: 49 files, 51.0M
composer
이번에phantomjs php의 프로그램 라이브러리를 사용하기 때문에compooser를 사용하여 패키지를 관리합니다.
composer.json의 내용
composer.json
{
"scripts": {
"post-install-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
],
"post-update-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
]
},
"require": {
"jonnyw/php-phantomjs": "4.*"
}
}
설치 결과NPC0201:sample livesense$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing twig/twig (v1.28.2)
Downloading: 100%
- Installing symfony/yaml (v3.2.0)
Downloading: 100%
- Installing symfony/filesystem (v3.2.0)
Downloading: 100%
- Installing symfony/dependency-injection (v3.2.0)
Downloading: 100%
- Installing symfony/config (v3.2.0)
Downloading: 100%
- Installing jakoch/phantomjs-installer (2.1.1)
Downloading: 100%
- Installing jonnyw/php-phantomjs (v4.5.1)
Downloading: 100%
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
symfony/dependency-injection suggests installing symfony/expression-language (For using expressions in service container configuration)
symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
Writing lock file
Generating autoload files
- Installing phantomjs (2.1.1)
Downloading: 100%
화면 캡처 코드(API)
PhantomJS로 화면을 간단하게 찍을 수 있어서 그 API를 만들어 봤어요.
index.php<?php
require __DIR__ . '/../vendor/autoload.php';
use JonnyW\PhantomJs\Client;
$web_url = $_GET['web_url'];
$output_file = '/data/' . sha1($_SERVER['REQUEST_URI']) . '.jpg';
if (file_exists($output_file)) {
header('Content-type: image/jpeg');
readfile($output_file);
} else {
// ファイルを分割してserviceクラスなどにまとめたほうがよい
$viewport_width = 1600;
$viewport_height = 1000;
$client = Client::getInstance();
$client->getEngine()->setPath('/usr/bin/phantomjs'); // phantomjs path
$request = $client->getMessageFactory()->createCaptureRequest(
$web_url, // url
'GET', // method
3000); // timeout(second)
$client->getEngine()->addOption('--proxy=host:port');
$request->setQuality(75); // default 75
$request->setCaptureDimensions($width, $height); // キャプチャ範囲
$request->setFormat('jpeg'); // pdf, png, jpeg, bmp, ppm, gif
$request->setOutputFile($output_file);
$request->setViewportSize($viewport_width, $viewport_height);
$response = $client->getMessageFactory()->createResponse();
$client->send($request, $response);
header('Content-type: image/jpeg');
readfile($output_file);
}
다음은 IESHIL 첫 페이지의 캡처입니다.
http://localhost/?web_url=https%3A//www.ieshil.com/
(왠지 영상이 잘 안 나온다)
참고 자료
<?php
require __DIR__ . '/../vendor/autoload.php';
use JonnyW\PhantomJs\Client;
$web_url = $_GET['web_url'];
$output_file = '/data/' . sha1($_SERVER['REQUEST_URI']) . '.jpg';
if (file_exists($output_file)) {
header('Content-type: image/jpeg');
readfile($output_file);
} else {
// ファイルを分割してserviceクラスなどにまとめたほうがよい
$viewport_width = 1600;
$viewport_height = 1000;
$client = Client::getInstance();
$client->getEngine()->setPath('/usr/bin/phantomjs'); // phantomjs path
$request = $client->getMessageFactory()->createCaptureRequest(
$web_url, // url
'GET', // method
3000); // timeout(second)
$client->getEngine()->addOption('--proxy=host:port');
$request->setQuality(75); // default 75
$request->setCaptureDimensions($width, $height); // キャプチャ範囲
$request->setFormat('jpeg'); // pdf, png, jpeg, bmp, ppm, gif
$request->setOutputFile($output_file);
$request->setViewportSize($viewport_width, $viewport_height);
$response = $client->getMessageFactory()->createResponse();
$client->send($request, $response);
header('Content-type: image/jpeg');
readfile($output_file);
}
Reference
이 문제에 관하여(PhantomJS를 사용하여 다양한 화면 캡처를 시도했습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ha-bottle/items/8dadd85a2fab35b7ecbf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)