5 use Shaarli\Config\ConfigManager
;
6 use WebThumbnailer\Application\ConfigManager
as WTConfigManager
;
7 use WebThumbnailer\WebThumbnailer
;
12 * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
16 protected const COMMON_MEDIA_DOMAINS
= [
34 public const MODE_ALL
= 'all';
35 public const MODE_COMMON
= 'common';
36 public const MODE_NONE
= 'none';
39 * @var WebThumbnailer instance.
44 * @var ConfigManager instance.
49 * Thumbnailer constructor.
51 * @param ConfigManager $conf instance.
53 public function __construct($conf)
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...
62 'php-gd extension must be loaded to use thumbnails. '
63 . 'Thumbnails are now disabled. Please reload the page.'
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'))
72 ->debug($this->conf
->get('dev.debug', false));
76 * Retrieve a thumbnail for given URL
78 * @param string $url where to look for a thumbnail.
80 * @return bool|string The thumbnail relative cache file path, or false if none has been found.
82 public function get($url)
85 $this->conf
->get('thumbnails.mode') === self
::MODE_COMMON
86 && ! $this->isCommonMediaOrImage($url)
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());
101 * We check weather the given URL is from a common media domain,
102 * or if the file extension is an image.
104 * @param string $url to check
106 * @return bool true if it's an image or from a common media domain, false otherwise.
108 public function isCommonMediaOrImage($url)
110 foreach (self
::COMMON_MEDIA_DOMAINS
as $domain) {
111 if (strpos($url, $domain) !== false) {
116 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
124 * Make sure that requirements are match to use thumbnails:
127 protected function checkRequirements()
129 return extension_loaded('gd');