Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed May 2, 2014
0 parents commit 1bb095f
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
12 changes: 12 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Convert a webpage to an image

[![Build Status](https://secure.travis-ci.org/freekmurze/browsershot.png)](http://travis-ci.org/freekmurze/geocoder)
[![Latest Stable Version](https://poser.pugx.org/spatie/browsershot/version.png)](https://packagist.org/packages/spatie/browsershot)
[![License](https://poser.pugx.org/spatie/browsershot/license.png)](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'
];
```
37 changes: 37 additions & 0 deletions composer.json
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"
}
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions phpsecp.yml
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
76 changes: 76 additions & 0 deletions spec/Spatie/Browsershot/BrowsershotSpec.php
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);
},
];
}


}
164 changes: 164 additions & 0 deletions src/Spatie/Browsershot/Browsershot.php
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;
}


}
15 changes: 15 additions & 0 deletions src/Spatie/Browsershot/BrowsershotServiceProvider.php
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 added src/Spatie/Browsershot/bin/phantomjs
Binary file not shown.
Empty file added tests/.gitkeep
Empty file.

0 comments on commit 1bb095f

Please sign in to comment.