take a screenshot of a url in php

One way to take a screenshot of a URL in PHP is to use a headless browser like PhantomJS. Here's an example code:

main.php
$url = 'https://example.com';
$output_file = '/path/to/output.png';

// Execute PhantomJS command
$command = "phantomjs rasterize.js $url $output_file";
$output = shell_exec($command);

// Check if output file exists
if (file_exists($output_file)) {
    echo "Screenshot saved to $output_file";
} else {
    echo "Failed to save screenshot";
}
344 chars
14 lines

Note that this code requires a rasterize.js file, which is a script that comes with the PhantomJS package. You can find it in the examples/ directory. Alternatively, you can use a pre-made PHP library that wraps around PhantomJS, such as jonnnnyw/php-phantomjs.

gistlibby LogSnag