From: Banana Date: Tue, 27 Dec 2022 12:15:37 +0000 (+0100) Subject: fixed thumbnail creation from google pagespeed X-Git-Tag: 2.8.1_20221231~7 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=d8891515a9cf30343a5a8c7f1bf20160c7bdaced;p=insipid.git fixed thumbnail creation from google pagespeed --- 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);