5 use Shaarli\Config\ConfigManager
;
6 use WebThumbnailer\Exception\WebThumbnailerException
;
7 use WebThumbnailer\WebThumbnailer
;
8 use WebThumbnailer\Application\ConfigManager
as WTConfigManager
;
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.enabled', false);
59 $this->conf
->write(true);
60 // TODO: create a proper error handling system able to catch exceptions...
61 die(t('php-gd extension must be loaded to use thumbnails. Thumbnails are now disabled. Please reload the page.'));
64 $this->wt
= new WebThumbnailer();
65 WTConfigManager
::addFile('inc/web-thumbnailer.json');
66 $this->wt
->maxWidth($this->conf
->get('thumbnails.width'))
67 ->maxHeight($this->conf
->get('thumbnails.height'))
69 ->debug($this->conf
->get('dev.debug', false));
73 * Retrieve a thumbnail for given URL
75 * @param string $url where to look for a thumbnail.
77 * @return bool|string The thumbnail relative cache file path, or false if none has been found.
79 public function get($url)
81 if ($this->conf
->get('thumbnails.mode') === self
::MODE_COMMON
82 && ! $this->isCommonMediaOrImage($url)
88 return $this->wt
->thumbnail($url);
89 } catch (WebThumbnailerException
$e) {
90 // Exceptions are only thrown in debug mode.
91 error_log(get_class($e) . ': ' . $e->getMessage());
97 * We check weather the given URL is from a common media domain,
98 * or if the file extension is an image.
100 * @param string $url to check
102 * @return bool true if it's an image or from a common media domain, false otherwise.
104 public function isCommonMediaOrImage($url)
106 foreach (self
::COMMON_MEDIA_DOMAINS
as $domain) {
107 if (strpos($url, $domain) !== false) {
112 if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
120 * Make sure that requirements are match to use thumbnails:
123 protected function checkRequirements()
125 return extension_loaded('gd');