@@ -40,122 +40,122 @@ |
||
40 | 40 | * @package OC\Preview |
41 | 41 | */ |
42 | 42 | class HEIC extends ProviderV2 { |
43 | - /** |
|
44 | - * {@inheritDoc} |
|
45 | - */ |
|
46 | - public function getMimeType(): string { |
|
47 | - return '/image\/hei(f|c)/'; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * {@inheritDoc} |
|
52 | - */ |
|
53 | - public function isAvailable(FileInfo $file): bool { |
|
54 | - return in_array('HEIC', \Imagick::queryFormats("HEI*")); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * {@inheritDoc} |
|
59 | - */ |
|
60 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
61 | - if (!$this->isAvailable($file)) { |
|
62 | - return null; |
|
63 | - } |
|
64 | - |
|
65 | - $tmpPath = $this->getLocalFile($file); |
|
66 | - if ($tmpPath === false) { |
|
67 | - \OC::$server->get(LoggerInterface::class)->error( |
|
68 | - 'Failed to get thumbnail for: ' . $file->getPath(), |
|
69 | - ['app' => 'core'] |
|
70 | - ); |
|
71 | - return null; |
|
72 | - } |
|
73 | - |
|
74 | - // Creates \Imagick object from the heic file |
|
75 | - try { |
|
76 | - $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); |
|
77 | - $bp->setFormat('jpg'); |
|
78 | - } catch (\Exception $e) { |
|
79 | - \OC::$server->get(LoggerInterface::class)->error( |
|
80 | - 'File: ' . $file->getPath() . ' Imagick says:', |
|
81 | - [ |
|
82 | - 'exception' => $e, |
|
83 | - 'app' => 'core', |
|
84 | - ] |
|
85 | - ); |
|
86 | - return null; |
|
87 | - } |
|
88 | - |
|
89 | - $this->cleanTmpFiles(); |
|
90 | - |
|
91 | - //new bitmap image object |
|
92 | - $image = new \OCP\Image(); |
|
93 | - $image->loadFromData((string) $bp); |
|
94 | - //check if image object is valid |
|
95 | - return $image->valid() ? $image : null; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Returns a preview of maxX times maxY dimensions in JPG format |
|
100 | - * |
|
101 | - * * The default resolution is already 72dpi, no need to change it for a bitmap output |
|
102 | - * * It's possible to have proper colour conversion using profileimage(). |
|
103 | - * ICC profiles are here: http://www.color.org/srgbprofiles.xalter |
|
104 | - * * It's possible to Gamma-correct an image via gammaImage() |
|
105 | - * |
|
106 | - * @param string $tmpPath the location of the file to convert |
|
107 | - * @param int $maxX |
|
108 | - * @param int $maxY |
|
109 | - * |
|
110 | - * @return \Imagick |
|
111 | - */ |
|
112 | - private function getResizedPreview($tmpPath, $maxX, $maxY) { |
|
113 | - $bp = new \Imagick(); |
|
114 | - |
|
115 | - // Layer 0 contains either the bitmap or a flat representation of all vector layers |
|
116 | - $bp->readImage($tmpPath . '[0]'); |
|
117 | - |
|
118 | - // Fix orientation from EXIF |
|
119 | - $bp->autoOrient(); |
|
120 | - |
|
121 | - $bp->setImageFormat('jpg'); |
|
122 | - |
|
123 | - $bp = $this->resize($bp, $maxX, $maxY); |
|
124 | - |
|
125 | - return $bp; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Returns a resized \Imagick object |
|
130 | - * |
|
131 | - * If you want to know more on the various methods available to resize an |
|
132 | - * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im |
|
133 | - * |
|
134 | - * @param \Imagick $bp |
|
135 | - * @param int $maxX |
|
136 | - * @param int $maxY |
|
137 | - * |
|
138 | - * @return \Imagick |
|
139 | - */ |
|
140 | - private function resize($bp, $maxX, $maxY) { |
|
141 | - [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry()); |
|
142 | - |
|
143 | - // We only need to resize a preview which doesn't fit in the maximum dimensions |
|
144 | - if ($previewWidth > $maxX || $previewHeight > $maxY) { |
|
145 | - // If we want a small image (thumbnail) let's be most space- and time-efficient |
|
146 | - if ($maxX <= 500 && $maxY <= 500) { |
|
147 | - $bp->thumbnailImage($maxY, $maxX, true); |
|
148 | - $bp->stripImage(); |
|
149 | - } else { |
|
150 | - // A bigger image calls for some better resizing algorithm |
|
151 | - // According to http://www.imagemagick.org/Usage/filter/#lanczos |
|
152 | - // the catrom filter is almost identical to Lanczos2, but according |
|
153 | - // to https://www.php.net/manual/en/imagick.resizeimage.php it is |
|
154 | - // significantly faster |
|
155 | - $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - return $bp; |
|
160 | - } |
|
43 | + /** |
|
44 | + * {@inheritDoc} |
|
45 | + */ |
|
46 | + public function getMimeType(): string { |
|
47 | + return '/image\/hei(f|c)/'; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * {@inheritDoc} |
|
52 | + */ |
|
53 | + public function isAvailable(FileInfo $file): bool { |
|
54 | + return in_array('HEIC', \Imagick::queryFormats("HEI*")); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * {@inheritDoc} |
|
59 | + */ |
|
60 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
61 | + if (!$this->isAvailable($file)) { |
|
62 | + return null; |
|
63 | + } |
|
64 | + |
|
65 | + $tmpPath = $this->getLocalFile($file); |
|
66 | + if ($tmpPath === false) { |
|
67 | + \OC::$server->get(LoggerInterface::class)->error( |
|
68 | + 'Failed to get thumbnail for: ' . $file->getPath(), |
|
69 | + ['app' => 'core'] |
|
70 | + ); |
|
71 | + return null; |
|
72 | + } |
|
73 | + |
|
74 | + // Creates \Imagick object from the heic file |
|
75 | + try { |
|
76 | + $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); |
|
77 | + $bp->setFormat('jpg'); |
|
78 | + } catch (\Exception $e) { |
|
79 | + \OC::$server->get(LoggerInterface::class)->error( |
|
80 | + 'File: ' . $file->getPath() . ' Imagick says:', |
|
81 | + [ |
|
82 | + 'exception' => $e, |
|
83 | + 'app' => 'core', |
|
84 | + ] |
|
85 | + ); |
|
86 | + return null; |
|
87 | + } |
|
88 | + |
|
89 | + $this->cleanTmpFiles(); |
|
90 | + |
|
91 | + //new bitmap image object |
|
92 | + $image = new \OCP\Image(); |
|
93 | + $image->loadFromData((string) $bp); |
|
94 | + //check if image object is valid |
|
95 | + return $image->valid() ? $image : null; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Returns a preview of maxX times maxY dimensions in JPG format |
|
100 | + * |
|
101 | + * * The default resolution is already 72dpi, no need to change it for a bitmap output |
|
102 | + * * It's possible to have proper colour conversion using profileimage(). |
|
103 | + * ICC profiles are here: http://www.color.org/srgbprofiles.xalter |
|
104 | + * * It's possible to Gamma-correct an image via gammaImage() |
|
105 | + * |
|
106 | + * @param string $tmpPath the location of the file to convert |
|
107 | + * @param int $maxX |
|
108 | + * @param int $maxY |
|
109 | + * |
|
110 | + * @return \Imagick |
|
111 | + */ |
|
112 | + private function getResizedPreview($tmpPath, $maxX, $maxY) { |
|
113 | + $bp = new \Imagick(); |
|
114 | + |
|
115 | + // Layer 0 contains either the bitmap or a flat representation of all vector layers |
|
116 | + $bp->readImage($tmpPath . '[0]'); |
|
117 | + |
|
118 | + // Fix orientation from EXIF |
|
119 | + $bp->autoOrient(); |
|
120 | + |
|
121 | + $bp->setImageFormat('jpg'); |
|
122 | + |
|
123 | + $bp = $this->resize($bp, $maxX, $maxY); |
|
124 | + |
|
125 | + return $bp; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Returns a resized \Imagick object |
|
130 | + * |
|
131 | + * If you want to know more on the various methods available to resize an |
|
132 | + * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im |
|
133 | + * |
|
134 | + * @param \Imagick $bp |
|
135 | + * @param int $maxX |
|
136 | + * @param int $maxY |
|
137 | + * |
|
138 | + * @return \Imagick |
|
139 | + */ |
|
140 | + private function resize($bp, $maxX, $maxY) { |
|
141 | + [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry()); |
|
142 | + |
|
143 | + // We only need to resize a preview which doesn't fit in the maximum dimensions |
|
144 | + if ($previewWidth > $maxX || $previewHeight > $maxY) { |
|
145 | + // If we want a small image (thumbnail) let's be most space- and time-efficient |
|
146 | + if ($maxX <= 500 && $maxY <= 500) { |
|
147 | + $bp->thumbnailImage($maxY, $maxX, true); |
|
148 | + $bp->stripImage(); |
|
149 | + } else { |
|
150 | + // A bigger image calls for some better resizing algorithm |
|
151 | + // According to http://www.imagemagick.org/Usage/filter/#lanczos |
|
152 | + // the catrom filter is almost identical to Lanczos2, but according |
|
153 | + // to https://www.php.net/manual/en/imagick.resizeimage.php it is |
|
154 | + // significantly faster |
|
155 | + $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + return $bp; |
|
160 | + } |
|
161 | 161 | } |