From: Banana Date: Fri, 20 Mar 2020 19:50:16 +0000 (+0100) Subject: more page screenshot stuff X-Git-Tag: 2.5_2020-03-21~4 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=b4726b0e3fc5cb8830da0636ff87975ef06427c2;p=insipid.git more page screenshot stuff --- diff --git a/webroot/config.default.php b/webroot/config.default.php index 724a2ea..3bd48da 100644 --- a/webroot/config.default.php +++ b/webroot/config.default.php @@ -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 diff --git a/webroot/lib/link.class.php b/webroot/lib/link.class.php index 0229b4c..f8b17df 100644 --- a/webroot/lib/link.class.php +++ b/webroot/lib/link.class.php @@ -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 */ diff --git a/webroot/lib/snapshot.class.php b/webroot/lib/snapshot.class.php index 1274d11..3d4b700 100644 --- a/webroot/lib/snapshot.class.php +++ b/webroot/lib/snapshot.class.php @@ -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 diff --git a/webroot/lib/summoner.class.php b/webroot/lib/summoner.class.php index a031a22..e67e1cb 100644 --- a/webroot/lib/summoner.class.php +++ b/webroot/lib/summoner.class.php @@ -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; + } } diff --git a/webroot/view/editlink.inc.php b/webroot/view/editlink.inc.php index c9aeecc..b0e8e23 100644 --- a/webroot/view/editlink.inc.php +++ b/webroot/view/editlink.inc.php @@ -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']); diff --git a/webroot/view/editlink.php b/webroot/view/editlink.php index 721b9ad..73b7b7b 100644 --- a/webroot/view/editlink.php +++ b/webroot/view/editlink.php @@ -131,6 +131,24 @@ + +
+
+

+ Full page screenshot. +

+
+
+ +

View page screenshot

+ + +
+
+

Tags: