]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Thumbnailer.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / Thumbnailer.php
1 <?php
2
3 namespace Shaarli;
4
5 use Shaarli\Config\ConfigManager;
6 use WebThumbnailer\Application\ConfigManager as WTConfigManager;
7 use WebThumbnailer\WebThumbnailer;
8
9 /**
10 * Class Thumbnailer
11 *
12 * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
13 */
14 class Thumbnailer
15 {
16 const COMMON_MEDIA_DOMAINS = [
17 'imgur.com',
18 'flickr.com',
19 'youtube.com',
20 'wikimedia.org',
21 'redd.it',
22 'gfycat.com',
23 'media.giphy.com',
24 'twitter.com',
25 'twimg.com',
26 'instagram.com',
27 'pinterest.com',
28 'pinterest.fr',
29 'soundcloud.com',
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.mode', Thumbnailer::MODE_NONE);
59 $this->conf->write(true);
60 // TODO: create a proper error handling system able to catch exceptions...
61 die(t(
62 'php-gd extension must be loaded to use thumbnails. '
63 . 'Thumbnails are now disabled. Please reload the page.'
64 ));
65 }
66
67 $this->wt = new WebThumbnailer();
68 WTConfigManager::addFile('inc/web-thumbnailer.json');
69 $this->wt->maxWidth($this->conf->get('thumbnails.width'))
70 ->maxHeight($this->conf->get('thumbnails.height'))
71 ->crop(true)
72 ->debug($this->conf->get('dev.debug', false));
73 }
74
75 /**
76 * Retrieve a thumbnail for given URL
77 *
78 * @param string $url where to look for a thumbnail.
79 *
80 * @return bool|string The thumbnail relative cache file path, or false if none has been found.
81 */
82 public function get($url)
83 {
84 if (
85 $this->conf->get('thumbnails.mode') === self::MODE_COMMON
86 && ! $this->isCommonMediaOrImage($url)
87 ) {
88 return false;
89 }
90
91 try {
92 return $this->wt->thumbnail($url);
93 } catch (\Throwable $e) {
94 // Exceptions are only thrown in debug mode.
95 error_log(get_class($e) . ': ' . $e->getMessage());
96 }
97 return false;
98 }
99
100 /**
101 * We check weather the given URL is from a common media domain,
102 * or if the file extension is an image.
103 *
104 * @param string $url to check
105 *
106 * @return bool true if it's an image or from a common media domain, false otherwise.
107 */
108 public function isCommonMediaOrImage($url)
109 {
110 foreach (self::COMMON_MEDIA_DOMAINS as $domain) {
111 if (strpos($url, $domain) !== false) {
112 return true;
113 }
114 }
115
116 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
117 return true;
118 }
119
120 return false;
121 }
122
123 /**
124 * Make sure that requirements are match to use thumbnails:
125 * - php-gd is loaded
126 */
127 protected function checkRequirements()
128 {
129 return extension_loaded('gd');
130 }
131 }