Forked from ajdruff/get webpage thumbnails from Google
Last active
December 16, 2020 09:34
-
-
Save hklcf/944aaa520b6917dc06e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"'); | |
?> |
Hi, thanks for sharing the code. is it possible to increase the screen shot size so text inside the image is readable and clear?
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']]);
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
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
For save to file