Skip to content

Commit

Permalink
added timeout feature
Browse files Browse the repository at this point in the history
  • Loading branch information
hotrush committed Feb 11, 2016
1 parent 1d78270 commit 2bf8395
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Take website's screenshots with PHP/PhantomJS and save them to PNG, JPG or PDF.

Also you can take a look at simple microservice with lumen and this package - [hotrush/Webshotter-microservice](https://github.com/hotrush/Webshotter-microservice).

## Changelog
***0.1.3*** - added ```timeout``` property that allow to limit page load timeout (using [onResourceTimeout](http://phantomjs.org/api/webpage/handler/on-resource-timeout.html) phantomjs feature). If timeout reached ```TimeoutException``` will be thrown.

## Installation

```
Expand All @@ -35,6 +38,7 @@ $jpg = $webshot
->setUrl('https://github.com')
->setWidth(1200)
->setHeight(800)
->setTimeout(5) // set timeout in seconds, 30 seconds default
->setFullPage(true) // set to true to get full page screenshot (width/height will be used for viewport only)
->saveToPng('github', $path);
```
Expand Down
8 changes: 8 additions & 0 deletions src/hotrush/Webshotter/Exception/InvalidDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace hotrush\Webshotter\Exception;

class InvalidDataException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/hotrush/Webshotter/Exception/TimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace hotrush\Webshotter\Exception;

class TimeoutException extends \Exception
{

}
45 changes: 37 additions & 8 deletions src/hotrush/Webshotter/Webshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace hotrush\Webshotter;

use Exception;
use hotrush\Webshotter\Exception\TimeoutException;
use hotrush\Webshotter\Exception\InvalidDataException;

class Webshot
{
Expand Down Expand Up @@ -48,6 +49,13 @@ class Webshot
*/
private $templatePath;

/**
* Page load timeout in sec
*
* @var int
*/
private $timeout;

/**
* Webshot constructor.
*
Expand All @@ -67,6 +75,7 @@ public function __construct($binPath = null, $templatePath = null)
$this->templatePath = realpath(dirname(__FILE__).'/../../views/webshotter.php');
}
$this->fullPage = false;
$this->timeout = 30;

return $this;
}
Expand All @@ -75,14 +84,14 @@ public function __construct($binPath = null, $templatePath = null)
* Set an url to take a shot
*
* @param $url
* @throws Exception
* @throws InvalidDataException
* @return $this
*/
public function setUrl($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL))
{
throw new Exception('Invalid url link');
throw new InvalidDataException('Invalid url link');
}

$this->url = $url;
Expand All @@ -95,13 +104,13 @@ public function setUrl($url)
*
* @param $width
* @return $this
* @throws Exception
* @throws InvalidDataException
*/
public function setWidth($width)
{
if (!is_numeric($width))
{
throw new Exception('Invalid width value');
throw new InvalidDataException('Invalid width value');
}

$this->width = (int) $width;
Expand All @@ -114,13 +123,13 @@ public function setWidth($width)
*
* @param $height
* @return $this
* @throws Exception
* @throws InvalidDataException
*/
public function setHeight($height)
{
if (!is_numeric($height))
{
throw new Exception('Invalid height value');
throw new InvalidDataException('Invalid height value');
}

$this->height = (int) $height;
Expand All @@ -142,6 +151,19 @@ public function setFullPage($fullPage = true)
return $this;
}

/**
* Set timeout value in sec
*
* @param $timeout
* @return $this
*/
public function setTimeout($timeout)
{
$this->timeout = (int) $timeout;

return $this;
}

/**
* Render PhantomJS script template
*
Expand All @@ -154,7 +176,8 @@ private function renderTemplate($filePath)
'url' => $this->url,
'width' => $this->width,
'height' => $this->height,
'fullPage' => $this->fullPage
'fullPage' => $this->fullPage,
'timeout' => $this->timeout * 1000, // convert into milliseconds
), $filePath);
}

Expand Down Expand Up @@ -220,6 +243,7 @@ public function saveToPdf($fileName, $filePath)
* @param $fileName
* @param $filePath
* @param $extension
* @throws TimeoutException
* @return string
*/
private function save($fileName, $filePath, $extension)
Expand All @@ -235,6 +259,11 @@ private function save($fileName, $filePath, $extension)
shell_exec($cmd);
fclose($tempExecutable);

if (!file_exists($fullPath))
{
throw new TimeoutException('Page load timeout.');
}

return $fullPath;
}
}
7 changes: 6 additions & 1 deletion src/views/webshotter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var page = new WebPage();
page.settings.javascriptEnabled = true;
page.onResourceTimeout = function(request) {
phantom.exit();
};
page.settings.resourceTimeout = <?php echo $timeout; ?>;

page.open('<?php echo $url; ?>', function (status) {
page.open('<?php echo $url; ?>', function () {
page.viewportSize = {
width: <?php echo $width; ?>,
height: <?php echo $height; ?>
Expand Down
14 changes: 11 additions & 3 deletions tests/PropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testWidthProperty()
{
$webshot = new Webshot();
$this->setExpectedException(
'Exception', 'Invalid width value'
'hotrush\Webshotter\Exception\InvalidDataException', 'Invalid width value'
);
$webshot->setWidth('foo');
$webshot->setWidth(1000);
Expand All @@ -29,7 +29,7 @@ public function testHeightProperty()
{
$webshot = new Webshot();
$this->setExpectedException(
'Exception', 'Invalid height value'
'hotrush\Webshotter\Exception\InvalidDataException', 'Invalid height value'
);
$webshot->setHeight('foo');
$webshot->setHeight(1000);
Expand All @@ -40,7 +40,7 @@ public function testUrlProperty()
{
$webshot = new Webshot();
$this->setExpectedException(
'Exception', 'Invalid url link'
'hotrush\Webshotter\Exception\InvalidDataException', 'Invalid url link'
);
$webshot->setUrl('foo-bar-url');
$webshot->setUrl('https://github.com');
Expand All @@ -55,6 +55,14 @@ public function testFullPageProperty()
$this->assertTrue(PHPUnit_Framework_Assert::readAttribute($webshot, 'fullPage'));
}

public function testTimeoutProperty()
{
$webshot = new Webshot();
$this->assertEquals(30, PHPUnit_Framework_Assert::readAttribute($webshot, 'timeout'));
$webshot->setTimeout(10);
$this->assertEquals(10, PHPUnit_Framework_Assert::readAttribute($webshot, 'timeout'));
}

public function testTemplateRendering()
{
$webshot = new Webshot();
Expand Down
1 change: 1 addition & 0 deletions tests/SaveToJpgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testImageJpgCreating()
->setUrl('https://github.com')
->setWidth(1200)
->setHeight(800)
->setTimeout(5)
->saveToJpg('github', $path);
$this->assertFileExists($jpg);
}
Expand Down
1 change: 1 addition & 0 deletions tests/SaveToPdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testImagePdfCreating()
->setUrl('https://github.com')
->setWidth(1200)
->setHeight(800)
->setTimeout(5)
->saveToPdf('github', $path);
$this->assertFileExists($pdf);
}
Expand Down
1 change: 1 addition & 0 deletions tests/SaveToPngTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testImagePngCreating()
->setUrl('https://github.com')
->setWidth(1200)
->setHeight(800)
->setTimeout(5)
->saveToPng('github', $path);
$this->assertFileExists($png);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TimeoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use hotrush\Webshotter\Webshot;

class TimeoutTest extends PHPUnit_Framework_TestCase
{
public function testTimeout()
{
$path = realpath(dirname(__FILE__).'/../tests/tmp/');
$webshot = new Webshot();
$this->setExpectedException(
'hotrush\Webshotter\Exception\TimeoutException', 'Page load timeout.'
);
$webshot
->setUrl('http://httpbin.org/delay/10')
->setWidth(1200)
->setHeight(800)
->setTimeout(5)
->saveToPng('delay', $path);
}
}
7 changes: 6 additions & 1 deletion tests/data/template.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var page = new WebPage();
page.settings.javascriptEnabled = true;
page.onResourceTimeout = function(request) {
phantom.exit();
};
page.settings.resourceTimeout = 30000;

page.open('https://github.com', function (status) {
page.open('https://github.com', function () {
page.viewportSize = {
width: 1200,
height: 800 };
Expand Down

0 comments on commit 2bf8395

Please sign in to comment.