Google Pagespeed v4可以使用 "screenshot "来对我们的报纸网站进行每日存档,直到本周三。我找不到任何信息,如果他们删除了对 "field=screenshot "的支持,但它不再返回JSON。是否有新的方法来捕捉截图,或者它已经消失了?
private function capture_thumbnail(){
if(false === $data = file_get_contents('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=' . $this->url . '&filter_third_party_resources=true&screenshot=true&strategy=desktop&fields=screenshot&key=' . $this->google_server_api_key))
throw new Exception('Could not get url contents');
$data = json_decode($data, true);
if(empty($data['screenshot']['data']))
throw new Exception('Screenshot data empty');
$image = str_replace(
array('_', '-'),
array('/', '+'),
$data['screenshot']['data']
);
$dir = SCREENSHOTS_DIR . date(CAPTURE_DIR_DATE_FORMAT) . '/';
if(false === file_exists($dir)){
if(false === mkdir($dir))
throw new Exception('Could not create screenshot directory');
}
if(false === file_put_contents($dir . date('H') . '.jpg', base64_decode($image)))
throw new Exception('Could not save image');
}
}
截图可以通过两种方式访问。
如果你只是想要最后的截图,它是 $data['lighthouseResult']['audits']['final-screenshot']
.
我已经在下面包含了相关的JSON,你可以看到它位于 "最终截图 "中,作为 ['details']['data']
.
"final-screenshot": {
"id": "final-screenshot",
"title": "Final Screenshot",
"description": "The last screenshot captured of the pageload.",
"score": null,
"scoreDisplayMode": "informative",
"details": {
"timing": 1396,
"timestamp": 1155417931434,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...etc...==",
"type": "screenshot"
}
},
如果你想让缩略图显示加载进度,那就是 $data['lighthouseResult']['audits']['screenshot-thumbnails']
我已经再次包含了相关的JSON数据,这样你就可以看到它是如何结构化的,但我将把这些留给你来解码。
"screenshot-thumbnails": {
"id": "screenshot-thumbnails",
"title": "Screenshot Thumbnails",
"description": "This is what the load of your site looked like.",
"score": null,
"scoreDisplayMode": "informative",
"details": {
"type": "filmstrip",
"items": [
{
"timing": 300,
"timestamp": 1155416835809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJ..etc.."
},
{
"timestamp": 1155417135809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJdsAPmRx/xKrPgA...etc...RLP//Z",
"timing": 600
},
{
"timestamp": 1155417435809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...etc...XItzzvtHoujj/iVWfAAMKHA9SOa9GPwoyl8TLbnAqiWf//Z",
"timing": 900
},
{
"timing": 1200,
"timestamp": 1155417735809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA...etc....qiWf//Z"
},
{
"timestamp": 1155418035809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...etc...sIbgvYpnjchJuJieTvPP41yLc877R6Lo4/4lVnwADChwPUjmvRj8KMpfEy25wKoln//2Q==",
"timing": 1500
},
{
"timing": 1800,
"timestamp": 1155418335809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA...etc....ADChwPUjmvRj8KMpfEy25wKoln//2Q=="
},
{
"timing": 2100,
"timestamp": 1155418635809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQA...etc...vPP41yLc877R6Lo4/4lVnwADChwPUjmvRj8KMpfEy25wKoln//2Q=="
},
{
"timing": 2400,
"timestamp": 1155418935809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA...etc...Ey25wKoln//2Q=="
},
{
"timing": 2700,
"timestamp": 1155419235809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQA...etc...6Lo4/4lVnwADChwPUjmvRj8KMpfEy25wKoln//2Q=="
},
{
"timing": 3000,
"timestamp": 1155419535809,
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAchJ...etc...VnwADChwPUjmvRj8KMpfEy25wKoln//2Q=="
}
],
"scale": 3000
}
},