@@ -49,6 +49,13 @@ abstract class Bitmap extends ProviderV2 {
4949 */
5050 abstract protected function getAllowedMimeTypes (): string ;
5151
52+ /**
53+ * @return list<string>
54+ */
55+ abstract protected function getMagicStrings (): array ;
56+
57+ abstract protected function getImagickFormatHint (): string ;
58+
5259 /**
5360 * {@inheritDoc}
5461 */
@@ -104,15 +111,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
104111 private function getResizedPreview ($ tmpPath , $ maxX , $ maxY ) {
105112 $ bp = new Imagick ();
106113
114+ if (!$ this ->isMagicStringSupported ($ tmpPath )) {
115+ throw new \Exception ('Invalid image type: magic string not recognized ' );
116+ }
117+
107118 // Validate mime type
108- $ bp ->pingImage ($ tmpPath . '[0] ' );
119+ $ bp ->pingImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
109120 $ mimeType = $ bp ->getImageMimeType ();
110121 if (!preg_match ($ this ->getAllowedMimeTypes (), $ mimeType )) {
111122 throw new \Exception ('File mime type does not match the preview provider: ' . $ mimeType );
112123 }
113124
114125 // Layer 0 contains either the bitmap or a flat representation of all vector layers
115- $ bp ->readImage ($ tmpPath . '[0] ' );
126+ $ bp ->readImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
116127
117128 $ bp = $ this ->resize ($ bp , $ maxX , $ maxY );
118129
@@ -121,6 +132,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
121132 return $ bp ;
122133 }
123134
135+ private function isMagicStringSupported (string $ filepath ): bool {
136+ $ signatures = $ this ->getMagicStrings ();
137+ if (empty ($ signatures )) {
138+ return true ;
139+ }
140+ $ length = array_reduce ($ signatures , static fn (int $ carry , string $ signature ) => max ($ carry , strlen ($ signature )), 0 );
141+ $ firstBytes = file_get_contents ($ filepath , false , null , 0 , $ length );
142+ foreach ($ signatures as $ signature ) {
143+ if (str_starts_with ($ firstBytes , $ signature )) {
144+ return true ;
145+ }
146+ }
147+
148+ return false ;
149+ }
150+
124151 /**
125152 * Returns a resized \Imagick object
126153 *
0 commit comments