hugsbrugs/php-screenshot

There is no license information available for the latest version (dev-master) of this package.

PHP Screenshot Utilities

dev-master 2020-01-06 15:21 UTC

This package is auto-updated.

Last update: 2024-12-07 02:27:13 UTC


README

This librairy provides utilities function to ease screenshot generation with different providers in order to always get a fallback in case first provider fails to generate screenshot.

Build Status Coverage Status

Notes

Currently 3 providers are available : Manet Apercite PagePeeker

You're welcome to suggest others providers.

Install

Install package with composer

composer require hugsbrugs/php-screenshot

In your PHP code, load library

require_once __DIR__ . '/../vendor/autoload.php';
use Hug\Screenshot\Screenshot as Screenshot;

You first have to define 3 constants

define('HUG_SCREENSHOT_SAVE_PATH', '/path/to/screenshots/');
define('HUG_SCREENSHOT_PROVIDERS', '/path/to/providers.json');
define('HUG_SCREENSHOT_CACHE', 'P1W');

HUG_SCREENSHOT_SAVE_PATH should be writable by webserver user

HUG_SCREENSHOT_PROVIDERS is a json file like following

{
	"1": {
		"provider":"Manet",
		"basic_auth_user":"USERNAME",
		"basic_auth_pass":"PASSWORD",
		"scheme":"http OR https",
		"host":"HOSTNAME_OR_IP",
		"port":"PORT",
		"params":{
			"engine":"slimerjs|phantomjs",
			"format":"jpg|png|gif",
			"width":"1024",
			"delay":"3000",
			"quality":"0.9"
		}
	},
	"2":{
		"provider":"Apercite"
	},
	"3":{
		"provider":"PagePeeker",
		"code":"YOUR_CODE",
		"entrypoint":"free",
		"size":"x",
		"refresh":"",
		"wait":""
	}
}

Please referer to each provider documentation for option details.

Usage

$Screenshot = new Screenshot();

$url = 'https://hugo.maugey.fr';
$widths = ['1024', '768', '480'];
$screenshot = $Screenshot->shot($url, $widths);
error_log(print_r($screenshot, true));

with outputs

[status] => success
[message] => 
[images] => Array
    (
        [1024] => hugo.maugey.fr-1024xauto-1492020456.jpg
        [768] => hugo.maugey.fr-768xauto-1492020467.jpg
        [480] => hugo.maugey.fr-480xauto-1492020475.jpg
    )
[details] => Array

Screenshot filenames are generated with following conventions :

  • URL
  • width x height
  • timestamp

Unit Tests

phpunit --bootstrap vendor/autoload.php tests

Author

Hugo Maugey visit my website ;)