123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- declare(strict_types=1);
- namespace OC\Preview;
- use OCP\Files\File;
- use OCP\Files\FileInfo;
- use OCP\IImage;
- use Psr\Log\LoggerInterface;
- class HEIC extends ProviderV2 {
-
- public function getMimeType(): string {
- return '/image\/(x-)?hei(f|c)/';
- }
-
- public function isAvailable(FileInfo $file): bool {
- return in_array('HEIC', \Imagick::queryFormats("HEI*"));
- }
-
- public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
- if (!$this->isAvailable($file)) {
- return null;
- }
- $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);
- $bp->setFormat('jpg');
- } catch (\Exception $e) {
- \OC::$server->get(LoggerInterface::class)->error(
- '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('/^image\/(x-)?(png|jpeg|gif|bmp|tiff|webp|hei(f|c)|avif)$/', $mimeType)) {
- throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
- }
-
- $bp->readImage($tmpPath . '[0]');
-
- $bp->autoOrient();
- $bp->setImageFormat('jpg');
- $bp = $this->resize($bp, $maxX, $maxY);
- return $bp;
- }
-
- private function resize($bp, $maxX, $maxY) {
- [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
-
- if ($previewWidth > $maxX || $previewHeight > $maxY) {
-
- if ($maxX <= 500 && $maxY <= 500) {
- $bp->thumbnailImage($maxY, $maxX, true);
- $bp->stripImage();
- } else {
-
-
-
-
-
- $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
- }
- }
- return $bp;
- }
- }
|