From d8891515a9cf30343a5a8c7f1bf20160c7bdaced Mon Sep 17 00:00:00 2001 From: Banana Date: Tue, 27 Dec 2022 13:15:37 +0100 Subject: [PATCH] fixed thumbnail creation from google pagespeed --- webroot/lib/link.class.php | 12 ++++++++++++ webroot/lib/snapshot.class.php | 28 ++++++++++++++++++++-------- webroot/lib/summoner.class.php | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/webroot/lib/link.class.php b/webroot/lib/link.class.php index 46da7f0..47b14a7 100644 --- a/webroot/lib/link.class.php +++ b/webroot/lib/link.class.php @@ -278,11 +278,23 @@ class Link { # decide to store or remove the image if (isset($data['localImage'])) { $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg'; + if(DEBUG) { + error_log("DEBUG Try to save local image to: $image"); + } if ($data['localImage'] === true) { + if(DEBUG) { + error_log("DEBUG want to save local image to: $image"); + } if (!file_exists($image) || $_imageUrlChanged === true) { + if(DEBUG) { + error_log("DEBUG Image new or not there yet: $image"); + } Summoner::downloadFile($data['image'], $image); } } elseif ($data['localImage'] === false) { + if(DEBUG) { + error_log("DEBUG Image to be removed: $image"); + } if (file_exists($image)) { unlink($image); } diff --git a/webroot/lib/snapshot.class.php b/webroot/lib/snapshot.class.php index d7fdb40..19cc5a2 100644 --- a/webroot/lib/snapshot.class.php +++ b/webroot/lib/snapshot.class.php @@ -58,19 +58,31 @@ class Snapshot { public function doSnapshot(string $url, string $filename): bool { $ret = false; - // new path in jason - // lighthouseResult audits full-page-screenshot details screenshot data (base64 encoded) - if(!empty($url) && is_writable(dirname($filename))) { + if(DEBUG) { + error_log("DEBUG try to save to $filename with $this->_googlePageSpeed for $url"); + } $theCall = Summoner::curlCall($this->_googlePageSpeed.urlencode($url).'&screenshot=true'); if(!empty($theCall)) { $jsonData = json_decode($theCall,true); - if(!empty($jsonData) && isset($jsonData['screenshot']['data'])) { - $imageData = $jsonData['screenshot']['data']; - $imageData = str_replace(['_', '-'], ['/', '+'], $imageData); - $imageData = base64_decode($imageData); - $ret = file_put_contents($filename, $imageData); + if(DEBUG) { + error_log("DEBUG Call result data: ".var_export($jsonData, true)); + } + if(!empty($jsonData) && isset($jsonData['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data'])) { + $imageData = $jsonData['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data']; + + $source = fopen($imageData, 'r'); + $destination = fopen($filename, 'w'); + if(stream_copy_to_stream($source, $destination)) { + $ret = $filename; + } + fclose($source); + fclose($destination); + } elseif(DEBUG) { + error_log("DEBUG invalid json data. Path ['lighthouseResult']['audits']['full-page-screenshot']['details']['screenshot']['data'] not found in : ".var_export($jsonData, true)); } + } elseif(DEBUG) { + error_log("DEBUG curl call failed"); } } diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index 4efd73c..a26b229 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -150,7 +150,7 @@ class Summoner { curl_setopt($ch, CURLOPT_USERAGENT,self::BROWSER_AGENT_STRING); // curl_setopt($ch, CURLOPT_VERBOSE, true); - //curl_setopt($ch, CURLOPT_HEADER, true); + // curl_setopt($ch, CURLOPT_HEADER, true); if(!empty($port)) { curl_setopt($ch, CURLOPT_PORT, $port); -- 2.39.5