]> 91.132.146.200 Git - insipid.git/commitdiff
fixed thumbnail creation from google pagespeed
authorBanana <mail@bananas-playground.net>
Tue, 27 Dec 2022 12:15:37 +0000 (13:15 +0100)
committerBanana <mail@bananas-playground.net>
Tue, 27 Dec 2022 12:15:37 +0000 (13:15 +0100)
webroot/lib/link.class.php
webroot/lib/snapshot.class.php
webroot/lib/summoner.class.php

index 46da7f016b809ca41b949c1600dd80308082e1fb..47b14a773db57f6791211e702aee1ada9a1f14d7 100644 (file)
@@ -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);
                                                }
index d7fdb40f4bc274d257f775e1164f94272416bd23..19cc5a25cd64d0ee0875a031b1c33fcd1131dc99 100644 (file)
@@ -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");
                        }
                }
 
index 4efd73ccdab21f1010a729c19c115bc9d84d49c9..a26b22971636c77147413b48749c97d8775a2e46 100644 (file)
@@ -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);