Thumbnailer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Shaarli;
  3. use Shaarli\Config\ConfigManager;
  4. use WebThumbnailer\Exception\WebThumbnailerException;
  5. use WebThumbnailer\WebThumbnailer;
  6. use WebThumbnailer\Application\ConfigManager as WTConfigManager;
  7. /**
  8. * Class Thumbnailer
  9. *
  10. * Utility class used to retrieve thumbnails using web-thumbnailer dependency.
  11. */
  12. class Thumbnailer
  13. {
  14. const COMMON_MEDIA_DOMAINS = [
  15. 'imgur.com',
  16. 'flickr.com',
  17. 'youtube.com',
  18. 'wikimedia.org',
  19. 'redd.it',
  20. 'gfycat.com',
  21. 'media.giphy.com',
  22. 'twitter.com',
  23. 'twimg.com',
  24. 'instagram.com',
  25. 'pinterest.com',
  26. 'pinterest.fr',
  27. 'tumblr.com',
  28. 'deviantart.com',
  29. ];
  30. const MODE_ALL = 'all';
  31. const MODE_COMMON = 'common';
  32. const MODE_NONE = 'none';
  33. /**
  34. * @var WebThumbnailer instance.
  35. */
  36. protected $wt;
  37. /**
  38. * @var ConfigManager instance.
  39. */
  40. protected $conf;
  41. /**
  42. * Thumbnailer constructor.
  43. *
  44. * @param ConfigManager $conf instance.
  45. */
  46. public function __construct($conf)
  47. {
  48. $this->conf = $conf;
  49. if (! $this->checkRequirements()) {
  50. $this->conf->set('thumbnails.enabled', false);
  51. $this->conf->write(true);
  52. // TODO: create a proper error handling system able to catch exceptions...
  53. die(t(
  54. 'php-gd extension must be loaded to use thumbnails. '
  55. .'Thumbnails are now disabled. Please reload the page.'
  56. ));
  57. }
  58. $this->wt = new WebThumbnailer();
  59. WTConfigManager::addFile('inc/web-thumbnailer.json');
  60. $this->wt->maxWidth($this->conf->get('thumbnails.width'))
  61. ->maxHeight($this->conf->get('thumbnails.height'))
  62. ->crop(true)
  63. ->debug($this->conf->get('dev.debug', false));
  64. }
  65. /**
  66. * Retrieve a thumbnail for given URL
  67. *
  68. * @param string $url where to look for a thumbnail.
  69. *
  70. * @return bool|string The thumbnail relative cache file path, or false if none has been found.
  71. */
  72. public function get($url)
  73. {
  74. if ($this->conf->get('thumbnails.mode') === self::MODE_COMMON
  75. && ! $this->isCommonMediaOrImage($url)
  76. ) {
  77. return false;
  78. }
  79. try {
  80. return $this->wt->thumbnail($url);
  81. } catch (WebThumbnailerException $e) {
  82. // Exceptions are only thrown in debug mode.
  83. error_log(get_class($e) . ': ' . $e->getMessage());
  84. }
  85. return false;
  86. }
  87. /**
  88. * We check weather the given URL is from a common media domain,
  89. * or if the file extension is an image.
  90. *
  91. * @param string $url to check
  92. *
  93. * @return bool true if it's an image or from a common media domain, false otherwise.
  94. */
  95. public function isCommonMediaOrImage($url)
  96. {
  97. foreach (self::COMMON_MEDIA_DOMAINS as $domain) {
  98. if (strpos($url, $domain) !== false) {
  99. return true;
  100. }
  101. }
  102. if (endsWith($url, '.jpg') || endsWith($url, '.png') || endsWith($url, '.jpeg')) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Make sure that requirements are match to use thumbnails:
  109. * - php-gd is loaded
  110. */
  111. protected function checkRequirements()
  112. {
  113. return extension_loaded('gd');
  114. }
  115. }