Capture Website Screenshots or Convert HTML to Images

PHP API

Create perfect image screenshots of websites or convert HTML straight to images by using the following features of GrabzIt's PHP API. However before you start remember that after calling the URLToImage, HTMLToImage or FileToImage methods the Save or SaveTo method must be called to create the image.

Basic Options

Only one parameter is required in order to take a screenshot of a web page or convert HTML into a image as shown in the following example.

$grabzIt->URLToImage("https://www.tesla.com");
//Then call the Save or SaveTo method
$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>");
//Then call the Save or SaveTo method
$grabzIt->FileToImage("example.html");
//Then call the Save or SaveTo method

Image Formats

GrabzIt's PHP API can create images in many formats, including JPG, PNG, WEBP, BMP (8 bit, 16 bit, 24 bit or 32 bit) and TIFF. The default format for images is JPG. However the quality of a JPG image may not be good enough for some applications in these circumstances the PNG format is recommended for image screenshots as it gives a good balance between quality and file size. The below example shows an image being created using the PNG format.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
            
$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");

Browser Size

The browser size refers to the size of the browser window that will be used when capturing the screenshot in most cases this does not need to be set as the default browser size will be sufficent for all most all tasks. To set the browser size just pass a value to the setBrowserWidth and setBrowserHeight methods of the GrabzItImageOptions class.

Change Image Size

Changing the size of an image is easy, doing it without distorting the image is a bit harder. To make the whole process simpler we recommend you use this simple image dimension calculator.

If you want to increase the image width and height to a size larger than the browser width and height, which by default is 1366 by 728 pixels, the browser width and height must also be increased to match.

Custom Identifier

You can pass a custom identifier to the image methods as shown below, this value is then returned to your GrabzIt PHP handler. For instance this custom identifier could be a database identifier, allowing a screenshot or image to be associated with a particular database record.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");

Full Length Screenshot

GrabzIt allows you to take a full length screenshot of an entire web page to do this you need to pass a -1 to the setBrowserHeight method. To ensure the image matches the size of the browser this pass a -1 to the setHeight and setWidth methods.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");

You can also return screenshots that are not cropped, but beware this can create large images. To do this pass a -1 to the setHeight and/or setWidth methods. Any dimension that is passed a -1 will not be cropped.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
Note there is no full length browser width!

Using these special values means that you can create a screenshot that is a full scale version of the entire web page if you wish!

Take a Screenshot of a Page Element

GrabzIt allows you to take a screenshot of a HTML element, such as a div or span tag, and capture all its content. To do this the HTML element you want to screenshot must be specified as a CSS selector.

...
<div id="features">
	<img src="http://www.example.com/peace.jpg"/><h3>World Peace Announced</h3>
</div>
...

For the example below we will select the div with the id "features" and output it as a 250 x 250px JPEG image.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The 250 parameters indicates that image should be sized to 250 x 250 px
$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(250);
$options->setHeight(250);
$options->setFormat("jpg");
$options->setTargetElement("#features");

$grabzIt->URLToImage("http://www.bbc.co.uk/news", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");

The next example takes another screenshot of the "features" div but this time outputs a JPEG image that is the exact size of the div.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The -1 indicates that image should not be cropped
$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);
$options->setBrowserHeight(-1);
$options->setFormat("jpg");
$options->setTargetElement("#features");

$grabzIt->URLToImage("http://www.bbc.co.uk/news", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");