/browsershot

Convert a webpage to an image

Primary LanguagePHPMIT LicenseMIT

Convert a webpage to an image

Latest Version Software License Build Status Total Downloads

The package can convert a webpage to an image. To accomplish this conversion Phantomjs (included in the project) is used.

This package is used to generate the sitepreviews on the homepage of spatie.be. It is also used by Gordon Murray to add previews to shared content.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

This package can be installed through Composer.

composer require spatie/browsershot

When using Laravel there is a service provider that you can make use of.

// app/config/app.php

'providers' => [
    '...',
    'Spatie\Browsershot\BrowsershotServiceProvider'
];

Please note that the provided binary is intented for use on Ubuntu.

Usage

Here is a sample call to create an image of a webpage:

    $browsershot = new Spatie\Browsershot\Browsershot();
    $browsershot
        ->setURL('http://www.arstechnica.com')
        ->setWidth(1024)
        ->setHeight(768)
        ->setTimeout(5000)
        ->save('targetdirectory/arstechnica-browsershot.jpg');

These methods are provided:

  • setBinPath(): Specify the path to your own phantomjs-binary.
  • setWidth(): Set the width of the image (defaults to 640).
  • setHeight(): Set the height of the image (defaults to 480).
  • setQuality(): Set the quality of the image (defaults to 60).
  • setHeightToRenderWholePage(): Calling this method will result in the entire webpage being rendered.
  • setURL(): Set the URL of the webpage which should be converted to an image
  • setTimeout(): Set the browsershot timeout duration in ms required to fully load all page assets and scripts (defaults to 5000).
  • setBackgroundColor($hexValueOrColorName): Set the background color of the html document prior to taking a screenshot.
  • save($targetFile): Starts the conversion-process. The targetfile should have one of these extensions: png, jpg, jpeg.

Other implementations

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

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