Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hklcf/944aaa520b6917dc06e0 to your computer and use it in GitHub Desktop.
Save hklcf/944aaa520b6917dc06e0 to your computer and use it in GitHub Desktop.
<?php
/**
* Get Google Page Speed Screenshot
*
* Uses Google's Page Speed API to generate a screenshot of a website.
* Returns the image as a base64 jpeg image tag
*
* Usage Example:
* echo getGooglePageSpeedScreenshot("http://ghost.org", 'class="thumbnail"');
*
*
* @author jaseclamp <https://gist.github.com/jaseclamp>
* @author <[email protected]>
* @link https://gist.github.com/simpliwp/e6b69e3eb5a3bc1dc081#file-get-webpage-thumbnails-from-google
* @link https://gist.github.com/jaseclamp/d4ac6205db352e822ff6
* #ref: http://stackoverflow.com/a/22342840/3306354
*
*
* @param string $site The url of the site you want to capture
* @param string $img_tag_attributes The img tag attributes to add
* @return string A base64 coded jpg img tag. Simply echo it out wherever you want the image.
*/
function getGooglePageSpeedScreenshot($site, $img_tag_attributes = "border='1'") {
#initialize
$use_cache = false;
$apc_is_loaded = extension_loaded('apc');
#set $use_cache
if($apc_is_loaded) {
apc_fetch("thumbnail:".$site, $use_cache);
}
if(!$use_cache) {
$image = file_get_contents("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=$site&screenshot=true");
$image = json_decode($image, true);
$image = $image['screenshot']['data'];
if($apc_is_loaded) {
apc_add("thumbnail:".$site, $image, 2400);
}
}
$image = str_replace(array('_', '-'), array('/', '+'), $image);
return "<img src=\"data:image/jpeg;base64,".$image."\" $img_tag_attributes />";
}
echo getGooglePageSpeedScreenshot($_GET['url'], 'class="thumbnail"');
?>
@dusta
Copy link

dusta commented Sep 20, 2017

For save to file

$imgBase64 = str_replace('data:image/jpeg;base64,', '', $image);
$imgBase64 = str_replace(' ', '+', $imgBase64);
file_put_contents('file.jpg', base64_decode($imgBase64));

@hozyali
Copy link

hozyali commented Jun 12, 2018

Hi, thanks for sharing the code. is it possible to increase the screen shot size so text inside the image is readable and clear?

@yookoala
Copy link

yookoala commented Aug 29, 2018

A version that provides more dynamic attributes injection flexibility.
https://gist.github.com/yookoala/6ccba4adfd0b3ba59fb2a1d9e4393099

Example usage:

// change width height
// Output: <img src="..." class="thumbnail" style="width: 100px; height: 100px" />
echo getGooglePageSpeedScreenshot($_GET['url'], ['class' => 'thumbnail', 'style' => ['width: 100px;', 'height: 100px;']]);

// entirely depend on CSS class
// Output: <img src="..." class="thumbnail custom-size" />
echo getGooglePageSpeedScreenshot($_GET['url'], ['class' => ['thumbnail', 'custom-size']]);

@Murad92
Copy link

Murad92 commented Sep 19, 2018

For save to file

$imgBase64 = str_replace('data:image/jpeg;base64,', '', $image);
$imgBase64 = str_replace(' ', '+', $imgBase64);
file_put_contents('file.jpg', base64_decode($imgBase64));

it is not right

@Murad92
Copy link

Murad92 commented Sep 19, 2018

sometimes this google api not working correctly and not do screenshot. Everybody can tell why???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment