]>
Commit | Line | Data |
---|---|---|
1b93137e A |
1 | <?php |
2 | ||
e85b7a05 A |
3 | namespace Shaarli; |
4 | ||
5 | use Shaarli\Config\ConfigManager; | |
dea72c71 | 6 | use WebThumbnailer\Application\ConfigManager as WTConfigManager; |
1b93137e A |
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 | { | |
b99e00f7 | 16 | protected const COMMON_MEDIA_DOMAINS = [ |
b302b3c5 A |
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', | |
0b631e69 | 29 | 'soundcloud.com', |
b302b3c5 A |
30 | 'tumblr.com', |
31 | 'deviantart.com', | |
32 | ]; | |
33 | ||
b99e00f7 A |
34 | public const MODE_ALL = 'all'; |
35 | public const MODE_COMMON = 'common'; | |
36 | public const MODE_NONE = 'none'; | |
b302b3c5 | 37 | |
1b93137e A |
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; | |
787faa42 A |
56 | |
57 | if (! $this->checkRequirements()) { | |
5bd62b5d | 58 | $this->conf->set('thumbnails.mode', Thumbnailer::MODE_NONE); |
787faa42 A |
59 | $this->conf->write(true); |
60 | // TODO: create a proper error handling system able to catch exceptions... | |
9d9f6d75 V |
61 | die(t( |
62 | 'php-gd extension must be loaded to use thumbnails. ' | |
53054b2b | 63 | . 'Thumbnails are now disabled. Please reload the page.' |
9d9f6d75 | 64 | )); |
787faa42 A |
65 | } |
66 | ||
1b93137e | 67 | $this->wt = new WebThumbnailer(); |
e85b7a05 | 68 | WTConfigManager::addFile('inc/web-thumbnailer.json'); |
1b93137e A |
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 | { | |
53054b2b A |
84 | if ( |
85 | $this->conf->get('thumbnails.mode') === self::MODE_COMMON | |
b302b3c5 A |
86 | && ! $this->isCommonMediaOrImage($url) |
87 | ) { | |
88 | return false; | |
89 | } | |
90 | ||
e85b7a05 A |
91 | try { |
92 | return $this->wt->thumbnail($url); | |
6132d647 | 93 | } catch (\Throwable $e) { |
e85b7a05 | 94 | // Exceptions are only thrown in debug mode. |
b302b3c5 | 95 | error_log(get_class($e) . ': ' . $e->getMessage()); |
e85b7a05 | 96 | } |
b302b3c5 A |
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; | |
1b93137e | 121 | } |
787faa42 A |
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 | } | |
1b93137e | 131 | } |