aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Thumbnailer.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2018-07-28 09:41:29 +0200
committerGitHub <noreply@github.com>2018-07-28 09:41:29 +0200
commitad5f47adbaee1eef85e90950ab8a45fe82959924 (patch)
treed23a186661db00d36cb2b2287a7bf890fbc62cfb /application/Thumbnailer.php
parent8fdd65b88412a0db28c723a486650c434fe5668c (diff)
parent7b4fea0e39be9e74e9aef13e73af9bbd2b1a6397 (diff)
downloadShaarli-ad5f47adbaee1eef85e90950ab8a45fe82959924.tar.gz
Shaarli-ad5f47adbaee1eef85e90950ab8a45fe82959924.tar.zst
Shaarli-ad5f47adbaee1eef85e90950ab8a45fe82959924.zip
Merge pull request #687 from ArthurHoaro/web-thumb
Use web-thumbnailer to retrieve thumbnails
Diffstat (limited to 'application/Thumbnailer.php')
-rw-r--r--application/Thumbnailer.php127
1 files changed, 127 insertions, 0 deletions
diff --git a/application/Thumbnailer.php b/application/Thumbnailer.php
new file mode 100644
index 00000000..7d0d9c33
--- /dev/null
+++ b/application/Thumbnailer.php
@@ -0,0 +1,127 @@
1<?php
2
3namespace Shaarli;
4
5use Shaarli\Config\ConfigManager;
6use WebThumbnailer\Exception\WebThumbnailerException;
7use WebThumbnailer\WebThumbnailer;
8use WebThumbnailer\Application\ConfigManager as WTConfigManager;
9
10/**
11 * Class Thumbnailer
12 *
13 * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
14 */
15class Thumbnailer
16{
17 const COMMON_MEDIA_DOMAINS = [
18 'imgur.com',
19 'flickr.com',
20 'youtube.com',
21 'wikimedia.org',
22 'redd.it',
23 'gfycat.com',
24 'media.giphy.com',
25 'twitter.com',
26 'twimg.com',
27 'instagram.com',
28 'pinterest.com',
29 'pinterest.fr',
30 'tumblr.com',
31 'deviantart.com',
32 ];
33
34 const MODE_ALL = 'all';
35 const MODE_COMMON = 'common';
36 const MODE_NONE = 'none';
37
38 /**
39 * @var WebThumbnailer instance.
40 */
41 protected $wt;
42
43 /**
44 * @var ConfigManager instance.
45 */
46 protected $conf;
47
48 /**
49 * Thumbnailer constructor.
50 *
51 * @param ConfigManager $conf instance.
52 */
53 public function __construct($conf)
54 {
55 $this->conf = $conf;
56
57 if (! $this->checkRequirements()) {
58 $this->conf->set('thumbnails.enabled', false);
59 $this->conf->write(true);
60 // TODO: create a proper error handling system able to catch exceptions...
61 die(t('php-gd extension must be loaded to use thumbnails. Thumbnails are now disabled. Please reload the page.'));
62 }
63
64 $this->wt = new WebThumbnailer();
65 WTConfigManager::addFile('inc/web-thumbnailer.json');
66 $this->wt->maxWidth($this->conf->get('thumbnails.width'))
67 ->maxHeight($this->conf->get('thumbnails.height'))
68 ->crop(true)
69 ->debug($this->conf->get('dev.debug', false));
70 }
71
72 /**
73 * Retrieve a thumbnail for given URL
74 *
75 * @param string $url where to look for a thumbnail.
76 *
77 * @return bool|string The thumbnail relative cache file path, or false if none has been found.
78 */
79 public function get($url)
80 {
81 if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON
82 && ! $this->isCommonMediaOrImage($url)
83 ) {
84 return false;
85 }
86
87 try {
88 return $this->wt->thumbnail($url);
89 } catch (WebThumbnailerException $e) {
90 // Exceptions are only thrown in debug mode.
91 error_log(get_class($e) . ': ' . $e->getMessage());
92 }
93 return false;
94 }
95
96 /**
97 * We check weather the given URL is from a common media domain,
98 * or if the file extension is an image.
99 *
100 * @param string $url to check
101 *
102 * @return bool true if it's an image or from a common media domain, false otherwise.
103 */
104 public function isCommonMediaOrImage($url)
105 {
106 foreach (self::COMMON_MEDIA_DOMAINS as $domain) {
107 if (strpos($url, $domain) !== false) {
108 return true;
109 }
110 }
111
112 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
113 return true;
114 }
115
116 return false;
117 }
118
119 /**
120 * Make sure that requirements are match to use thumbnails:
121 * - php-gd is loaded
122 */
123 protected function checkRequirements()
124 {
125 return extension_loaded('gd');
126 }
127}