@@ -33,8 +33,12 @@ abstract class Bitmap extends ProviderV2 {
3333 abstract protected function getAllowedMimeTypes (): string ;
3434
3535 /**
36- * {@inheritDoc}
36+ * @return list<string>
3737 */
38+ abstract protected function getMagicStrings (): array ;
39+
40+ abstract protected function getImagickFormatHint (): string ;
41+
3842 #[\Override]
3943 public function getThumbnail (File $ file , int $ maxX , int $ maxY ): ?IImage {
4044 $ tmpPath = $ this ->getLocalFile ($ file );
@@ -65,7 +69,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
6569 //new bitmap image object
6670 $ image = new Image ();
6771 $ image ->loadFromData ((string )$ bp );
68- //check if image object is valid
72+ // Check if image object is valid
6973 return $ image ->valid () ? $ image : null ;
7074 }
7175
@@ -88,15 +92,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8892 private function getResizedPreview ($ tmpPath , $ maxX , $ maxY ) {
8993 $ bp = new Imagick ();
9094
95+ if (!$ this ->isMagicStringSupported ($ tmpPath )) {
96+ throw new \Exception ('Invalid image type: magic string not recognized ' );
97+ }
98+
9199 // Validate mime type
92- $ bp ->pingImage ($ tmpPath . '[0] ' );
100+ $ bp ->pingImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
93101 $ mimeType = $ bp ->getImageMimeType ();
94102 if (!preg_match ($ this ->getAllowedMimeTypes (), $ mimeType )) {
95103 throw new \Exception ('File mime type does not match the preview provider: ' . $ mimeType );
96104 }
97105
98106 // Layer 0 contains either the bitmap or a flat representation of all vector layers
99- $ bp ->readImage ($ tmpPath . '[0] ' );
107+ $ bp ->readImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
100108
101109 $ bp = $ this ->resize ($ bp , $ maxX , $ maxY );
102110
@@ -105,6 +113,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
105113 return $ bp ;
106114 }
107115
116+ private function isMagicStringSupported (string $ filepath ): bool {
117+ $ signatures = $ this ->getMagicStrings ();
118+ if (empty ($ signatures )) {
119+ return true ;
120+ }
121+ $ length = array_reduce ($ signatures , static fn (int $ carry , string $ signature ) => max ($ carry , strlen ($ signature )), 0 );
122+ $ firstBytes = file_get_contents ($ filepath , false , null , 0 , $ length );
123+ foreach ($ signatures as $ signature ) {
124+ if (str_starts_with ($ firstBytes , $ signature )) {
125+ return true ;
126+ }
127+ }
128+
129+ return false ;
130+ }
131+
108132 /**
109133 * Returns a resized \Imagick object
110134 *
0 commit comments