]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Thumbnailer.php
7d0d9c33203f9724afdde1b3db102d54aaa53d2b
[github/shaarli/Shaarli.git] / application / Thumbnailer.php
1 <?php
2
3 namespace Shaarli;
4
5 use Shaarli\Config\ConfigManager;
6 use WebThumbnailer\Exception\WebThumbnailerException;
7 use WebThumbnailer\WebThumbnailer;
8 use WebThumbnailer\Application\ConfigManager as WTConfigManager;
9
10 /**
11 * Class Thumbnailer
12 *
13 * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
14 */
15 class 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 }