]> 91.132.146.200 Git - insipid.git/commitdiff
more page screenshot stuff
authorBanana <mail@bananas-playground.net>
Fri, 20 Mar 2020 19:50:16 +0000 (20:50 +0100)
committerBanana <mail@bananas-playground.net>
Fri, 20 Mar 2020 19:50:16 +0000 (20:50 +0100)
webroot/config.default.php
webroot/lib/link.class.php
webroot/lib/snapshot.class.php
webroot/lib/summoner.class.php
webroot/view/editlink.inc.php
webroot/view/editlink.php

index 724a2ea5133e5585bfdf650c1412688fd9ae3261..3bd48da6cf260deffbc2b6900f31b6363977ab29 100644 (file)
@@ -71,3 +71,7 @@ define('EMAIL_REPORT_BACK',false);
 define('EMAIL_REPLY_BACK_VALID','');
 define('EMAIL_REPLY_BACK_ADDRESS','');
 define('EMAIL_REPLY_BACK_SUBJECT','Insipid email import response');
+
+# Use wkhtmltopdf to create a whole page screenshot of a given link
+define('WKHTMLTOPDF_USE',false);
+define('WKHTMLTOPDF_COMMAND','/absolute/path/to/wkhtmltoimage');
\ No newline at end of file
index 0229b4ca8a43f55c4d35f743da685a93df04f362..f8b17df9340b668117c1181fe8d69e1751ae32fe 100644 (file)
@@ -76,6 +76,7 @@ class Link {
                                $this->_image();
                                $this->_private();
                                $this->_snapshot();
+                               $this->_pageScreenshot();
                        }
                }
 
@@ -233,7 +234,7 @@ class Link {
 
                                # decide to store or remove the image
                                if (isset($data['localImage'])) {
-                                       $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'];
+                                       $image = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/thumbnail-' . $this->_data['hash'].'.jpg';
                                        if ($data['localImage'] === true) {
                                                if (!file_exists($image) || $_imageUrlChanged === true) {
                                                        Summoner::downloadFile($data['image'], $image);
@@ -247,7 +248,7 @@ class Link {
 
                                # decide if we want to make a local snapshot
                                if(isset($data['snapshot'])) {
-                                       $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'];
+                                       $snapshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/snapshot-' . $this->_data['hash'].'.jpg';
                                        if ($data['snapshot'] === true) {
                                                if (!file_exists($snapshot) || $_imageUrlChanged === true) {
                                                        require_once 'lib/snapshot.class.php';
@@ -264,6 +265,25 @@ class Link {
                                        }
                                }
 
+                               # decide if we want to make a local full page scrrenshot
+                               if(isset($data['pagescreenshot'])) {
+                                       $pagescreenshot = ABSOLUTE_PATH . '/' . LOCAL_STORAGE . '/pagescreenshot-' . $this->_data['hash'].'.jpg';
+                                       if ($data['pagescreenshot'] === true) {
+                                               if (!file_exists($pagescreenshot) || $_imageUrlChanged === true) {
+                                                       require_once 'lib/snapshot.class.php';
+                                                       $snap = new Snapshot();
+                                                       $do = $snap->wholePageSnpashot($this->_data['link'], $pagescreenshot);
+                                                       if(empty($do)) {
+                                                               error_log('ERROR Failed to create snapshot: '.var_export($data,true));
+                                                       }
+                                               }
+                                       } elseif ($data['pagescreenshot'] === false) {
+                                               if (file_exists($pagescreenshot)) {
+                                                       unlink($pagescreenshot);
+                                               }
+                                       }
+                               }
+
 
                                $ret = true;
                        } else {
@@ -285,6 +305,7 @@ class Link {
                $this->_removeCategoryRelation(false);
                $this->_deleteImage();
                $this->_deleteSnapshot();
+               $this->_deletePageScreenshot();
        }
 
        /**
@@ -391,28 +412,42 @@ class Link {
        private function _image() {
                if (!empty($this->_data['hash'])) {
                        $this->_data['imageToShow'] = $this->_data['image'];
-                       $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'];
+                       $image = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
                        if (file_exists($image)) {
-                               $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'];
+                               $this->_data['imageToShow'] = LOCAL_STORAGE.'/thumbnail-'.$this->_data['hash'].'.jpg';
                                $this->_data['localImage'] = true;
                        }
                }
        }
 
        /**
-        * determine of we have a local stored snapshot
+        * determine if we have a local stored snapshot
         * if so populate the snapshotLink attribute
         */
        private function _snapshot() {
                if (!empty($this->_data['hash'])) {
-                       $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'];
+                       $snapshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
                        if (file_exists($snapshot)) {
-                               $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'];
+                               $this->_data['snapshotLink'] = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
                                $this->_data['snapshot'] = true;
                        }
                }
        }
 
+       /**
+        * determine if we have a local full page screenshot
+        * if so populate the pagescreenshotLink attribute
+        */
+       private function _pageScreenshot() {
+               if (!empty($this->_data['hash'])) {
+                       $pagescreenshot = ABSOLUTE_PATH.'/'.LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
+                       if (file_exists($pagescreenshot)) {
+                               $this->_data['pagescreenshotLink'] = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
+                               $this->_data['pagescreenshot'] = true;
+                       }
+               }
+       }
+
        /**
         * remove the local stored image
         */
@@ -426,17 +461,29 @@ class Link {
        }
 
        /**
-        * remove the local stored image
+        * remove the local stored snapshot
         */
        private function _deleteSnapshot() {
                if (!empty($this->_data['hash']) && !empty($this->_data['snapshotLink'])) {
-                       $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'];
+                       $snapshot = LOCAL_STORAGE.'/snapshot-'.$this->_data['hash'].'.jpg';
                        if (file_exists($snapshot)) {
                                unlink($snapshot);
                        }
                }
        }
 
+       /**
+        * remove the local stored pagescreenshot
+        */
+       private function _deletePageScreenshot() {
+               if (!empty($this->_data['hash']) && !empty($this->_data['pagescreenshotLink'])) {
+                       $pagescreenshot = LOCAL_STORAGE.'/pagescreenshot-'.$this->_data['hash'].'.jpg';
+                       if (file_exists($pagescreenshot)) {
+                               unlink($pagescreenshot);
+                       }
+               }
+       }
+
        /**
         * check if the status is private and set the info
         */
index 1274d11732309136efb2a750b5c64174304c7b0c..3d4b7003a0bcc212111f4c19d9f40499e225219f 100644 (file)
@@ -33,6 +33,7 @@
  */
 class Snapshot {
        private $_googlePageSpeed = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=';
+       private $_wkhtmltoimageOptions = '--load-error-handling ignore --quality 80 --quiet --width 1900';
 
        public function __constructor() {}
 
@@ -61,4 +62,23 @@ class Snapshot {
 
                return $ret;
        }
+
+       /**
+        * use configired WKHTMLTOPDF_COMMAND to create a whole page screenshot
+        * of the given link and store it locally
+        *
+        * @param String $url URL to take the screenshot from
+        * @return
+        */
+       public function wholePageSnpashot($url,$filename) {
+               $ret = false;
+
+               if(!empty($url) && is_writable(dirname($filename))) {
+                       $cmd = WKHTMLTOPDF_COMMAND;
+                       $params = $this->_wkhtmltoimageOptions." ".$url." ".$filename;
+                       $run = Summoner::systemcall($cmd,$params);
+               }
+
+               return $ret;
+       }
 }
\ No newline at end of file
index a031a22a6bef30fd4b890376b498683bc0ec89fa..e67e1cb2ea5797071df66c8dc0428fafce081e9d 100644 (file)
@@ -635,4 +635,16 @@ class Summoner {
                        'status' => $status
                );
        }
+
+       /**
+        * just a very basis system call execution
+        * needs error handling and stuff
+        */
+       static function systemcall($command,$params) {
+               //escapeshellarg
+               $command = escapeshellcmd($command." ".$params);
+               exec($command,$return);
+
+               return $return;
+       }
 }
index c9aeecc566e4e48f682ba6062f2eaa2bb8208020..b0e8e23cf367291a3b3b0f0c041be889c28bf8c5 100644 (file)
@@ -111,6 +111,11 @@ if(isset($_POST['data']) && !empty($_POST['data']) && isset($_POST['editlink']))
                $formData['snapshot'] = true;
        }
 
+       $formData['pagescreenshot'] = false;
+       if(isset($fData['pagescreenshot'])) {
+               $formData['pagescreenshot'] = true;
+       }
+
        $formData['description'] = trim($fData['description']);
        $formData['title'] = trim($fData['title']);
        $formData['image'] = trim($fData['image']);
index 721b9ad2f75e22b04af11a528de27befce396b89..73b7b7b98902ae525d596aa45be156d4d88c6c59 100644 (file)
                                </label>
                        </div>
                </div>
+               <?php if(defined('WKHTMLTOPDF_USE') && WKHTMLTOPDF_USE === true) { ?>
+               <div class="columns">
+                       <div class="column is-one-quarter">
+                               <p>
+                                       Full page screenshot.
+                               </p>
+                       </div>
+                       <div class="column">
+                               <?php if(isset($linkData['pagescreenshotLink'])) { ?>
+                               <p><a href="<?php echo $linkData['pagescreenshotLink']; ?>" target="_blank">View page screenshot</a></p>
+                               <?php } ?>
+                               <label class="checkbox">
+                                       <input type="checkbox" name="data[pagescreenshot]" value="1" <?php if(Summoner::ifset($formData, 'pagescreenshot')) echo "checked"; ?>  />
+                                       Save a full page screenshot (This can take some time)
+                               </label>
+                       </div>
+               </div>
+               <?php } ?>
         <div class="columns">
             <div class="column is-one-quarter">
                 <p>Tags:</p>