GithubHelp home page GithubHelp logo

doc22940 / screen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from microweber-dev/screen

1.0 1.0 0.0 133.26 MB

Website screenshot tool based on PHP and PhantomJS

License: MIT License

PHP 96.65% JavaScript 3.35%

screen's Introduction

Screen

Web site screenshot tool based on PHP and PhantomJS You can use it to take screenshots for testing or monitoring service

Install

Via Composer

$ composer require microweber/screen

If on any unix system, you need to make the bin executable chmod +x /path/to/screen/bin/phantomjs

The directory /path/to/screen/jobs must be writeble as well.

Linux requirements

  • FontConfig - apt-get/yum install fontconfig
  • FreeType - apt-get/yum install freetype*

Usage

With this library you can make use of PhantomJs to screenshot a website.

Check our demo or read the following instructions.

Creating the object, you can either pass the url on the constructer or set it later on

use Screen\Capture;

$url = 'https://github.com';

$screenCapture = new Capture($url);
// or
$screenCapture = new Capture();
$screenCapture->setUrl($url);

You can also set the browser dimensions

$screenCapture->setWidth(1200);
$screenCapture->setHeight(800);

you can set also DOM Element Position (top, left)

$screenCapture->setTop(100);
$screenCapture->setLeft(100);

This will output all the page including the content rendered beyond the setted dimensions (e.g.: all the scrollable content), if you want just the content inside those boudaries you need to clip the result

// You also need to set the width and height.
$screenCapture->setClipWidth(1200);
$screenCapture->setClipHeight(800);

Some webpages don't have a background color setted to the body, if you want you can set the color using this method

$screenCapture->setBackgroundColor('#ffffff');

You can also set the User Agent

$screenCapture->setUserAgentString('Some User Agent String');

And the resulted image type

// allowed types are 'jpg' and 'png', default is 'jpg'.
$screenCapture->setImageType(Screen\Image\Types\Png::FORMAT);
// or
$screenCapture->setImageType('png');
  • If the format is jpg and the background color is not set, the default value will be #FFFFFF, if png the default background color will be transparent.

And most importantly, save the result

$fileLocation = '/some/dir/test.' . $screen->getImageType()->getFormat();
$screenCapture->save($fileLocation);

// you don't need to set the file extension
$fileLocation = '/some/dir/test';
$screenCapture->save($fileLocation); // Will automatically determine the extension type

echo $screenCapture->getImageLocation(); // --> /some/dir/test.png

Injection your own JS into the web page

You can also run your own JS scripts or snippets before the screenshot.

For that we have the method includeJs, here are some usage examples:

// Including a remote file
$jQueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';
$screenCapture->includeJs(new \Screen\Injection\Url($jQUeryUrl));

// Including a local file
$localFilePath = 'path/to/my/script.js';
$screenCapture->includeJs(new \Screen\Injection\LocalPath($localFilePath));

// Using the scripts included on the library
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideCookiesPolicy());
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideSignUp());

// Using a js snippet
$screen->includeJs("console.log('This is supa cool!');");

Just use this method before calling save(...)

Passing options to PhantomJS

You can set the options that will be passed to the PhantomJS binary.

$screenCapture->setOptions([
    'ignore-ssl-errors' => 'yes',
    // '--ignore-ssl-errors' => 'yes', // dashes may be omitted
]);

Other configurations

Additionally to the basic usage, you can set so extra configurations.

You can change the where the PhantomJS binary file is.

$screenCapture->binPath = '/path/to/bin/dir/';
// This will result in /path/to/bin/dir/phantomjs

Change the jobs location

$screenCapture->jobs->setLocation('/path/to/jobs/dir/');
echo $screenCapture->jobs->getLocation(); // -> /path/to/jobs/dir/

And set an output base location

$screenCapture->output->setLocation('/path/to/output/dir/');
echo $screenCapture->output->getLocation(); // -> /path/to/output/dir/

// if the output location is setted
$screenCapture->save('file.jpg');
// will save the file to /path/to/output/dir/file.jpg

You can also clean/delete all the generated job files like this:

$screenCapture->jobs->clean();

License

The MIT License (MIT). Please see License File for more information.

Credits

Thanks to the PhantomJS (LICENSE) guys for creating their awesome WebKit scripting interface.

This tool was originally created to take screenshots for Microweber

screen's People

Contributors

peter-mw avatar masnathan avatar limonte avatar mbardelmeijer avatar pouu69 avatar al0mie avatar ashleyadams avatar akiyamasm avatar joshp23 avatar karlonjegovan avatar lahaxearnaud avatar noahcoffey avatar alnviana avatar sam0hack avatar xuanskyer avatar

Stargazers

Acampbell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.