-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1bb095f
Showing
12 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: php | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- hhvm | ||
|
||
before_script: | ||
- composer install --prefer-source --no-interaction --dev | ||
|
||
script: vendor/bin/phpspec run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Freek Van der Herten <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Convert a webpage to an image | ||
|
||
[](http://travis-ci.org/freekmurze/geocoder) | ||
[](https://packagist.org/packages/spatie/browsershot) | ||
[](https://packagist.org/packages/spatie/browsershot) | ||
|
||
The package can convert a webpage to an image. | ||
|
||
## Installation | ||
|
||
You can install this package through Composer. | ||
|
||
```js | ||
{ | ||
"require": { | ||
"spatie/browsershot": "dev-master" | ||
} | ||
} | ||
``` | ||
|
||
When using Laravel there is a service provider that you can make use of. | ||
|
||
```php | ||
|
||
// app/config/app.php | ||
|
||
'providers' => [ | ||
'...', | ||
'Spatie\Browsershot\BrowsershotServiceProvider' | ||
]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "spatie/browsershot", | ||
"description": "Convert a webpage to an image", | ||
"keywords": | ||
[ | ||
"screenshot", | ||
"browser", | ||
"convert", | ||
"image", | ||
"phantomjs", | ||
"website", | ||
"laravel" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Freek Van der Herten", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0", | ||
"illuminate/support": "4.1.*", | ||
"intervention/image": "1.*" | ||
}, | ||
"bin": [ | ||
"src/Spatie/Browsershot/bin/phantomjs" | ||
], | ||
"require-dev": { | ||
"phpspec/phpspec": "2.0.*@dev" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Spatie\\Browsershot": "src/" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
suites: | ||
spatie_suite: | ||
namespace: Spatie | ||
src_path: src | ||
spec_prefix: spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace spec\Spatie\Browsershot; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
|
||
|
||
class BrowsershotSpec extends ObjectBehavior | ||
{ | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Spatie\Browsershot\Browsershot'); | ||
} | ||
|
||
function it_should_fail_if_target_file_is_not_set() | ||
{ | ||
$this->shouldThrow(new \Exception('targetfile not set'))->during('save', ['']); | ||
} | ||
|
||
function it_should_fail_if_url_is_not_set() | ||
{ | ||
$this->shouldThrow(new \Exception('url not set'))->during('save', [$this->getTestPath()]); | ||
} | ||
|
||
function it_should_fail_if_binary_does_not_exist() | ||
{ | ||
$this | ||
->setURL($this->getTestURL()) | ||
->setBinPath('') | ||
->shouldThrow(new \Exception('binary does not exist')) | ||
->during('save', [$this->getTestPath()]); | ||
} | ||
|
||
function it_should_fail_if_invalid_url_is_set() | ||
{ | ||
$this | ||
->setURL('gibberish') | ||
->shouldThrow(new \Exception('url is invalid')) | ||
->during('save', [$this->getTestPath()]); | ||
} | ||
|
||
function it_should_create_an_image_if_run_succesfully() | ||
{ | ||
$this | ||
->setURL($this->getTestURL()) | ||
->save($this->getTestPath()); | ||
|
||
$this->shouldExist($this->getTestPath()); | ||
} | ||
|
||
|
||
public function getTestPath() | ||
{ | ||
return 'image.png'; | ||
} | ||
|
||
public function getTestURL() | ||
{ | ||
return 'http://google.com'; | ||
} | ||
|
||
|
||
public function getMatchers() | ||
{ | ||
return [ | ||
'exist' => function($subject, $file) { | ||
return file_exists($file); | ||
}, | ||
]; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
|
||
namespace Spatie\Browsershot; | ||
|
||
use Exception; | ||
use \Intervention\Image\Image; | ||
|
||
/** | ||
* Class Browsershot | ||
* @package Spatie\Browsershot | ||
*/ | ||
|
||
class Browsershot { | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $width; | ||
/** | ||
* @var int | ||
*/ | ||
private $height; | ||
/** | ||
* @var | ||
*/ | ||
private $URL; | ||
/** | ||
* @var string | ||
*/ | ||
private $binPath; | ||
|
||
|
||
/** | ||
* @param string $binPath The path to the phantomjs binary | ||
*/ | ||
public function __construct($binPath = '') | ||
{ | ||
|
||
if ($binPath == '') { | ||
$this->binPath = __DIR__ . '/bin/phantomjs'; | ||
} | ||
|
||
$this->width = 640; | ||
$this->height = 480; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $binPath The path to the phantomjs binary | ||
* @return $this | ||
*/ | ||
public function setBinPath($binPath) | ||
{ | ||
$this->binPath = $binPath; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param int $width The required with of the screenshot | ||
* @return $this | ||
* @throws \Exception | ||
*/ | ||
public function setWidth($width) | ||
{ | ||
if (! is_numeric($width)) { | ||
throw new Exception('Width must be numeric'); | ||
} | ||
|
||
$this->width = $width; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param int $height The required height of the screenshot | ||
* @return $this | ||
* @throws \Exception | ||
*/ | ||
public function setHeight($height) | ||
{ | ||
if (! is_numeric($height)) { | ||
throw new Exception('Height must be numeric'); | ||
} | ||
|
||
$this->height = $height; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $url The website of which a screenshot should be make | ||
* @return $this | ||
* @throws \Exception | ||
*/ | ||
public function setURL($url) | ||
{ | ||
if (! strlen($url) > 0 ) { | ||
throw new Exception('No url specified'); | ||
} | ||
|
||
$this->URL = $url; | ||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* Convert the webpage to an image | ||
* | ||
* @param string $targetFile The path of the file where the screenshot should be saved | ||
* @return bool | ||
* @throws \Exception | ||
*/ | ||
public function save($targetFile) | ||
{ | ||
if ($targetFile == '') { | ||
throw new Exception('targetfile not set'); | ||
} | ||
|
||
if (! file_exists($this->binPath)) { | ||
throw new Exception('binary does not exist'); | ||
} | ||
|
||
if ($this->URL == '') { | ||
throw new Exception('url not set'); | ||
} | ||
|
||
if (filter_var($this->URL, FILTER_VALIDATE_URL) === FALSE) { | ||
throw new Exception('url is invalid'); | ||
} | ||
|
||
$tempJsFileHandle = tmpfile(); | ||
|
||
$fileContent= " | ||
var page = require('webpage').create(); | ||
page.settings.javascriptEnabled = true; | ||
page.viewportSize = { width: " . $this->width . ", height: " . $this->height . " }; | ||
page.open('" . $this->URL . "', function() { | ||
window.setTimeout(function(){ | ||
page.render('" . $targetFile . "'); | ||
phantom.exit(); | ||
}, 5000); // give phantomjs 5 seconds to process all javascript | ||
});"; | ||
|
||
fwrite($tempJsFileHandle, $fileContent); | ||
$tempFileName = stream_get_meta_data($tempJsFileHandle)['uri']; | ||
$cmd = escapeshellcmd("{$this->binPath} " . $tempFileName); | ||
|
||
shell_exec($cmd); | ||
|
||
fclose($tempJsFileHandle); | ||
|
||
if (! file_exists($targetFile) OR filesize($targetFile) < 1024) | ||
{ | ||
throw new Exception('could not create screenshot'); | ||
} | ||
|
||
Image::make($targetFile) | ||
->crop($this->width, $this->height, 0, 0) | ||
->save($targetFile, 60); | ||
|
||
return true; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php namespace Spatie\Browsershot; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class BrowsershotServiceProvider extends ServiceProvider { | ||
|
||
public function register() | ||
{ | ||
$this->app->bind( | ||
'browsershot', | ||
'Spatie\Browsershot' | ||
); | ||
} | ||
|
||
} |
Binary file not shown.
Empty file.