]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Thumbnailer.php
d2284e795eb3b43261cfc4577832e610a390b036
[github/shaarli/Shaarli.git] / application / Thumbnailer.php
1 <?php
2
3 namespace Shaarli;
4
5 use Shaarli\Config\ConfigManager;
6 use WebThumbnailer\Exception\WebThumbnailerException;
7 use WebThumbnailer\WebThumbnailer;
8 use WebThumbnailer\Application\ConfigManager as WTConfigManager;
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;
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
43 $this->wt = new WebThumbnailer();
44 WTConfigManager::addFile('inc/web-thumbnailer.json');
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 {
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 }
67 }
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 }
77 }