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
= [
34 const MODE_ALL
= 'all';
35 const MODE_COMMON
= 'common';
36 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)
84 if ($this->conf
->get('thumbnails.mode') === self
::MODE_COMMON
85 && ! $this->isCommonMediaOrImage($url)
91 return $this->wt
->thumbnail($url);
92 } catch (WebThumbnailerException
$e) {
93 // Exceptions are only thrown in debug mode.
94 error_log(get_class($e) . ': ' . $e->getMessage());
100 * We check weather the given URL is from a common media domain,
101 * or if the file extension is an image.
103 * @param string $url to check
105 * @return bool true if it's an image or from a common media domain, false otherwise.
107 public function isCommonMediaOrImage($url)
109 foreach (self
::COMMON_MEDIA_DOMAINS
as $domain) {
110 if (strpos($url, $domain) !== false) {
115 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
123 * Make sure that requirements are match to use thumbnails:
126 protected function checkRequirements()
128 return extension_loaded('gd');