123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace OC\Preview;
- use Imagick;
- use OCP\Files\File;
- use OCP\IImage;
- use Psr\Log\LoggerInterface;
- abstract class Bitmap extends ProviderV2 {
-
- abstract protected function getAllowedMimeTypes(): string;
-
- public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
- $tmpPath = $this->getLocalFile($file);
- if ($tmpPath === false) {
- \OC::$server->get(LoggerInterface::class)->error(
- 'Failed to get thumbnail for: ' . $file->getPath(),
- ['app' => 'core']
- );
- return null;
- }
-
- try {
- $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
- } catch (\Exception $e) {
- \OC::$server->get(LoggerInterface::class)->info(
- 'File: ' . $file->getPath() . ' Imagick says:',
- [
- 'exception' => $e,
- 'app' => 'core',
- ]
- );
- return null;
- }
- $this->cleanTmpFiles();
-
- $image = new \OCP\Image();
- $image->loadFromData((string) $bp);
-
- return $image->valid() ? $image : null;
- }
-
- private function getResizedPreview($tmpPath, $maxX, $maxY) {
- $bp = new Imagick();
-
- $bp->pingImage($tmpPath . '[0]');
- $mimeType = $bp->getImageMimeType();
- if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
- throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
- }
-
- $bp->readImage($tmpPath . '[0]');
- $bp = $this->resize($bp, $maxX, $maxY);
- $bp->setImageFormat('png');
- return $bp;
- }
-
- private function resize($bp, $maxX, $maxY) {
- [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
-
- if ($previewWidth > $maxX || $previewHeight > $maxY) {
-
- $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
- }
- return $bp;
- }
- }
|