]>
Commit | Line | Data |
---|---|---|
1b93137e A |
1 | <?php |
2 | ||
e85b7a05 A |
3 | namespace Shaarli; |
4 | ||
5 | use Shaarli\Config\ConfigManager; | |
6 | use WebThumbnailer\Exception\WebThumbnailerException; | |
1b93137e | 7 | use WebThumbnailer\WebThumbnailer; |
e85b7a05 | 8 | use WebThumbnailer\Application\ConfigManager as WTConfigManager; |
1b93137e A |
9 | |
10 | /** | |
11 | * Class Thumbnailer | |
12 | * | |
13 | * Utility class used to retrieve thumbnails using web-thumbnailer dependency. | |
14 | */ | |
15 | class Thumbnailer | |
16 | { | |
17 | /** | |
18 | * @var WebThumbnailer instance. | |
19 | */ | |
20 | protected $wt; | |
21 | ||
22 | /** | |
23 | * @var ConfigManager instance. | |
24 | */ | |
25 | protected $conf; | |
26 | ||
27 | /** | |
28 | * Thumbnailer constructor. | |
29 | * | |
30 | * @param ConfigManager $conf instance. | |
31 | */ | |
32 | public function __construct($conf) | |
33 | { | |
34 | $this->conf = $conf; | |
787faa42 A |
35 | |
36 | if (! $this->checkRequirements()) { | |
37 | $this->conf->set('thumbnails.enabled', false); | |
38 | $this->conf->write(true); | |
39 | // TODO: create a proper error handling system able to catch exceptions... | |
40 | die(t('php-gd extension must be loaded to use thumbnails. Thumbnails are now disabled. Please reload the page.')); | |
41 | } | |
42 | ||
1b93137e | 43 | $this->wt = new WebThumbnailer(); |
e85b7a05 | 44 | WTConfigManager::addFile('inc/web-thumbnailer.json'); |
1b93137e A |
45 | $this->wt->maxWidth($this->conf->get('thumbnails.width')) |
46 | ->maxHeight($this->conf->get('thumbnails.height')) | |
47 | ->crop(true) | |
48 | ->debug($this->conf->get('dev.debug', false)); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Retrieve a thumbnail for given URL | |
53 | * | |
54 | * @param string $url where to look for a thumbnail. | |
55 | * | |
56 | * @return bool|string The thumbnail relative cache file path, or false if none has been found. | |
57 | */ | |
58 | public function get($url) | |
59 | { | |
e85b7a05 A |
60 | try { |
61 | return $this->wt->thumbnail($url); | |
62 | } catch (WebThumbnailerException $e) { | |
63 | // Exceptions are only thrown in debug mode. | |
64 | error_log(get_class($e) .': '. $e->getMessage()); | |
65 | return false; | |
66 | } | |
1b93137e | 67 | } |
787faa42 A |
68 | |
69 | /** | |
70 | * Make sure that requirements are match to use thumbnails: | |
71 | * - php-gd is loaded | |
72 | */ | |
73 | protected function checkRequirements() | |
74 | { | |
75 | return extension_loaded('gd'); | |
76 | } | |
1b93137e | 77 | } |