1
0

IMagickSupport.php 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Preview;
  7. use OCP\ICache;
  8. use OCP\ICacheFactory;
  9. class IMagickSupport {
  10. private ICache $cache;
  11. private ?\Imagick $imagick;
  12. public function __construct(ICacheFactory $cacheFactory) {
  13. $this->cache = $cacheFactory->createLocal('imagick');
  14. if (extension_loaded('imagick')) {
  15. $this->imagick = new \Imagick();
  16. } else {
  17. $this->imagick = null;
  18. }
  19. }
  20. public function hasExtension(): bool {
  21. return !is_null($this->imagick);
  22. }
  23. public function supportsFormat(string $format): bool {
  24. if (is_null($this->imagick)) {
  25. return false;
  26. }
  27. $cached = $this->cache->get($format);
  28. if (!is_null($cached)) {
  29. return $cached;
  30. }
  31. $formatSupported = count($this->imagick->queryFormats($format)) === 1;
  32. $this->cache->set($format, $cached);
  33. return $formatSupported;
  34. }
  35. }