Image.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Preview;
  8. use OCP\Files\File;
  9. use OCP\IImage;
  10. use OCP\Server;
  11. use Psr\Log\LoggerInterface;
  12. abstract class Image extends ProviderV2 {
  13. /**
  14. * {@inheritDoc}
  15. */
  16. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  17. $maxSizeForImages = \OC::$server->getConfig()->getSystemValueInt('preview_max_filesize_image', 50);
  18. $size = $file->getSize();
  19. if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
  20. return null;
  21. }
  22. $image = new \OCP\Image();
  23. $fileName = $this->getLocalFile($file);
  24. if ($fileName === false) {
  25. Server::get(LoggerInterface::class)->error(
  26. 'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
  27. ['app' => 'core']
  28. );
  29. return null;
  30. }
  31. $image->loadFromFile($fileName);
  32. $image->fixOrientation();
  33. $this->cleanTmpFiles();
  34. if ($image->valid()) {
  35. $image->scaleDownToFit($maxX, $maxY);
  36. return $image;
  37. }
  38. return null;
  39. }
  40. }