Skip to content

Instantly share code, notes, and snippets.

@yookoala
Forked from hklcf/Screenshot via Google PageSpeed API
Last active June 16, 2019 15:25
Show Gist options
  • Save yookoala/6ccba4adfd0b3ba59fb2a1d9e4393099 to your computer and use it in GitHub Desktop.
Save yookoala/6ccba4adfd0b3ba59fb2a1d9e4393099 to your computer and use it in GitHub Desktop.
Screenshot via Google PageSpeed API
<?php
include_once 'getGooglePageSpeedScreenshot.php';
echo getGooglePageSpeedScreenshot($_GET['url']);
// Output: <img src="..." border="1" style="width: 80px; height: 80px" />
echo getGooglePageSpeedScreenshot($_GET['url'], [
'class' => 'thumbnail',
'style' => ['width: 80px;', 'height: 80px;']
]);
// Output: <img src="..." class="thumbnail" style="width: 80px; height: 80px" />
echo getGooglePageSpeedScreenshot($_GET['url'], [
'class' => ['thumbnail', 'custom-class'],
'aria-label' => 'A Thumbnail',
]);
// Output: <img src="..." class="thumbnail custom-class" aria-label="A Thumbnail" />
<?php
/**
* http_build_attribute
* Build HTML tag attributes string with a given array.
* @param array $attr
* An assoc array of attributes with key as attribute name
* and value as attribute value(s).
*
* The value can be either an array of string or a string.
* If an object is given, it will be cast as string.
*
* @return string
*/
function http_build_attributes(array $attr): string
{
return implode(' ', array_map(function ($key) use ($attr) {
$value = is_object($attr[$key]) ? (string) $attr[$key] : $attr[$key];
$value = is_array($value) ? implode(' ', array_map(function ($item) {
return (string) $item; // cast all second level array value to string.
}, $value)) : (string) $value;
return sprintf('%s="%s"', htmlspecialchars($key), htmlspecialchars($value));
}, array_keys($attr)));
}
/**
* 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, $attributes = ['border' => '1', 'style' => ['width: 80px;', 'height: 80px;']])
{
if (empty(trim($site))) {
return NULL; // for empty $site, return nothing
}
// check cache
$apc_is_loaded = extension_loaded('apc');
if ($apc_is_loaded) {
$has_cache = false;
$cached = apc_fetch("thumbnail:" . $site, $has_cache);
if ($has_cache) return $cached;
}
// check $site for valid URL
if (filter_var($site, FILTER_VALIDATE_URL) === FALSE) {
throw new \Exception(sprintf('invalid URL: %s', $site));
return NULL;
}
// get pagespeed API response for the URL
$response = file_get_contents('https://www.googleapis.com/pagespeedonline/v1/runPagespeed?' . http_build_query([
'url' => (array_key_exists('path', parse_url($site))) ? dirname($site) : $site,
'screenshot' => 'true',
]));
$image = json_decode($response, true);
$image = $image['screenshot']['data'];
if ($apc_is_loaded) apc_add("thumbnail:" . $site, $image, 2400);
// render HTML tag for the image.
$image = str_replace(array('_', '-'), array('/', '+'), $image);
$html_attributes = http_build_attributes($attributes + [
'src' => 'data:image/jpeg;base64,' . $image,
]);
return "<img $html_attributes />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment