From 1b93137e16694f52952c930848e1a7928e8a00a6 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 9 Nov 2016 18:57:02 +0100 Subject: Use web-thumbnailer to retrieve thumbnails * requires PHP 5.6 * use blazy on linklist since a lot more thumbs are retrieved * thumbnails can be disabled * thumbs size is now 120x120 * thumbs are now cropped to fit the expected size Fixes #345 #425 #487 #543 #588 #590 --- application/Thumbnailer.php | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 application/Thumbnailer.php (limited to 'application/Thumbnailer.php') diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php new file mode 100644 index 00000000..b669adae --- /dev/null +++ b/application/Thumbnailer.php @@ -0,0 +1,49 @@ +conf = $conf; + $this->wt = new WebThumbnailer(); + \WebThumbnailer\Application\ConfigManager::addFile('inc/web-thumbnailer.json'); + $this->wt->maxWidth($this->conf->get('thumbnails.width')) + ->maxHeight($this->conf->get('thumbnails.height')) + ->crop(true) + ->debug($this->conf->get('dev.debug', false)); + } + + /** + * Retrieve a thumbnail for given URL + * + * @param string $url where to look for a thumbnail. + * + * @return bool|string The thumbnail relative cache file path, or false if none has been found. + */ + public function get($url) + { + return $this->wt->thumbnail($url); + } +} -- cgit v1.2.3 From e85b7a05a177f803ae36ba5c12835313f31177bc Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 11 Nov 2017 14:01:21 +0100 Subject: Update thumbnail integration after rebasing the branch --- application/Thumbnailer.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'application/Thumbnailer.php') diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php index b669adae..9cf5dacd 100644 --- a/application/Thumbnailer.php +++ b/application/Thumbnailer.php @@ -1,6 +1,11 @@ conf = $conf; $this->wt = new WebThumbnailer(); - \WebThumbnailer\Application\ConfigManager::addFile('inc/web-thumbnailer.json'); + WTConfigManager::addFile('inc/web-thumbnailer.json'); $this->wt->maxWidth($this->conf->get('thumbnails.width')) ->maxHeight($this->conf->get('thumbnails.height')) ->crop(true) @@ -44,6 +49,12 @@ class Thumbnailer */ public function get($url) { - return $this->wt->thumbnail($url); + try { + return $this->wt->thumbnail($url); + } catch (WebThumbnailerException $e) { + // Exceptions are only thrown in debug mode. + error_log(get_class($e) .': '. $e->getMessage()); + return false; + } } } -- cgit v1.2.3 From 787faa42f3a2bcbf83a7853f23f3667a6febf9da Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 6 Apr 2018 18:21:47 +0200 Subject: Take code review into account Upgrade web-thumbnailer and display thumbs right after download --- application/Thumbnailer.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'application/Thumbnailer.php') diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php index 9cf5dacd..d2284e79 100644 --- a/application/Thumbnailer.php +++ b/application/Thumbnailer.php @@ -32,6 +32,14 @@ class Thumbnailer public function __construct($conf) { $this->conf = $conf; + + if (! $this->checkRequirements()) { + $this->conf->set('thumbnails.enabled', false); + $this->conf->write(true); + // TODO: create a proper error handling system able to catch exceptions... + die(t('php-gd extension must be loaded to use thumbnails. Thumbnails are now disabled. Please reload the page.')); + } + $this->wt = new WebThumbnailer(); WTConfigManager::addFile('inc/web-thumbnailer.json'); $this->wt->maxWidth($this->conf->get('thumbnails.width')) @@ -57,4 +65,13 @@ class Thumbnailer return false; } } + + /** + * Make sure that requirements are match to use thumbnails: + * - php-gd is loaded + */ + protected function checkRequirements() + { + return extension_loaded('gd'); + } } -- cgit v1.2.3 From b302b3c584b84f22f0e6f187b072180ecbacdfab Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 5 Jul 2018 20:29:55 +0200 Subject: Thumbnails: add a common mode to only retrieve thumbs from popular media websites --- application/Thumbnailer.php | 54 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'application/Thumbnailer.php') diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php index d2284e79..7d0d9c33 100644 --- a/application/Thumbnailer.php +++ b/application/Thumbnailer.php @@ -14,6 +14,27 @@ use WebThumbnailer\Application\ConfigManager as WTConfigManager; */ class Thumbnailer { + const COMMON_MEDIA_DOMAINS = [ + 'imgur.com', + 'flickr.com', + 'youtube.com', + 'wikimedia.org', + 'redd.it', + 'gfycat.com', + 'media.giphy.com', + 'twitter.com', + 'twimg.com', + 'instagram.com', + 'pinterest.com', + 'pinterest.fr', + 'tumblr.com', + 'deviantart.com', + ]; + + const MODE_ALL = 'all'; + const MODE_COMMON = 'common'; + const MODE_NONE = 'none'; + /** * @var WebThumbnailer instance. */ @@ -57,13 +78,42 @@ class Thumbnailer */ public function get($url) { + if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON + && ! $this->isCommonMediaOrImage($url) + ) { + return false; + } + try { return $this->wt->thumbnail($url); } catch (WebThumbnailerException $e) { // Exceptions are only thrown in debug mode. - error_log(get_class($e) .': '. $e->getMessage()); - return false; + error_log(get_class($e) . ': ' . $e->getMessage()); } + return false; + } + + /** + * We check weather the given URL is from a common media domain, + * or if the file extension is an image. + * + * @param string $url to check + * + * @return bool true if it's an image or from a common media domain, false otherwise. + */ + public function isCommonMediaOrImage($url) + { + foreach (self::COMMON_MEDIA_DOMAINS as $domain) { + if (strpos($url, $domain) !== false) { + return true; + } + } + + if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) { + return true; + } + + return false; } /** -- cgit v1.2.3