@@ -34,8 +34,12 @@ abstract class Bitmap extends ProviderV2 {
3434 abstract protected function getAllowedMimeTypes (): string ;
3535
3636 /**
37- * {@inheritDoc}
37+ * @return list<string>
3838 */
39+ abstract protected function getMagicStrings (): array ;
40+
41+ abstract protected function getImagickFormatHint (): string ;
42+
3943 #[\Override]
4044 public function getThumbnail (File $ file , int $ maxX , int $ maxY ): ?IImage {
4145 $ tmpPath = $ this ->getLocalFile ($ file );
@@ -66,7 +70,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
6670 //new bitmap image object
6771 $ image = new Image ();
6872 $ image ->loadFromData ((string )$ bp );
69- //check if image object is valid
73+ // Check if image object is valid
7074 return $ image ->valid () ? $ image : null ;
7175 }
7276
@@ -89,15 +93,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8993 private function getResizedPreview ($ tmpPath , $ maxX , $ maxY ) {
9094 $ bp = new Imagick ();
9195
96+ if (!$ this ->isMagicStringSupported ($ tmpPath )) {
97+ throw new \Exception ('Invalid image type: magic string not recognized ' );
98+ }
99+
92100 // Validate mime type
93- $ bp ->pingImage ($ tmpPath . '[0] ' );
101+ $ bp ->pingImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
94102 $ mimeType = $ bp ->getImageMimeType ();
95103 if (!preg_match ($ this ->getAllowedMimeTypes (), $ mimeType )) {
96104 throw new \Exception ('File mime type does not match the preview provider: ' . $ mimeType );
97105 }
98106
99107 // Layer 0 contains either the bitmap or a flat representation of all vector layers
100- $ bp ->readImage ($ tmpPath . '[0] ' );
108+ $ bp ->readImage ($ this -> getImagickFormatHint () . ' : ' . $ tmpPath . '[0] ' );
101109
102110 $ bp = $ this ->resize ($ bp , $ maxX , $ maxY );
103111
@@ -106,6 +114,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
106114 return $ bp ;
107115 }
108116
117+ private function isMagicStringSupported (string $ filepath ): bool {
118+ $ signatures = $ this ->getMagicStrings ();
119+ if (empty ($ signatures )) {
120+ return true ;
121+ }
122+ $ length = array_reduce ($ signatures , static fn (int $ carry , string $ signature ) => max ($ carry , strlen ($ signature )), 0 );
123+ $ firstBytes = file_get_contents ($ filepath , false , null , 0 , $ length );
124+ foreach ($ signatures as $ signature ) {
125+ if (str_starts_with ($ firstBytes , $ signature )) {
126+ return true ;
127+ }
128+ }
129+
130+ return false ;
131+ }
132+
109133 /**
110134 * Returns a resized \Imagick object
111135 *
0 commit comments