5 use Shaarli\Config\ConfigManager
;
6 use WebThumbnailer\Application\ConfigManager
as WTConfigManager
;
7 use WebThumbnailer\Exception\WebThumbnailerException
;
8 use WebThumbnailer\WebThumbnailer
;
13 * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
17 const COMMON_MEDIA_DOMAINS
= [
35 const MODE_ALL
= 'all';
36 const MODE_COMMON
= 'common';
37 const MODE_NONE
= 'none';
40 * @var WebThumbnailer instance.
45 * @var ConfigManager instance.
50 * Thumbnailer constructor.
52 * @param ConfigManager $conf instance.
54 public function __construct($conf)
58 if (! $this->checkRequirements()) {
59 $this->conf
->set('thumbnails.mode', Thumbnailer
::MODE_NONE
);
60 $this->conf
->write(true);
61 // TODO: create a proper error handling system able to catch exceptions...
63 'php-gd extension must be loaded to use thumbnails. '
64 .'Thumbnails are now disabled. Please reload the page.'
68 $this->wt
= new WebThumbnailer();
69 WTConfigManager
::addFile('inc/web-thumbnailer.json');
70 $this->wt
->maxWidth($this->conf
->get('thumbnails.width'))
71 ->maxHeight($this->conf
->get('thumbnails.height'))
73 ->debug($this->conf
->get('dev.debug', false));
77 * Retrieve a thumbnail for given URL
79 * @param string $url where to look for a thumbnail.
81 * @return bool|string The thumbnail relative cache file path, or false if none has been found.
83 public function get($url)
85 if ($this->conf
->get('thumbnails.mode') === self
::MODE_COMMON
86 && ! $this->isCommonMediaOrImage($url)
92 return $this->wt
->thumbnail($url);
93 } catch (WebThumbnailerException
$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');