- * @param string $filename Image filename
- * @param boolean $fromcurl Check Content-Type header from curl request
- * @param string $header passed headers to take into account
+ * @param string $image_data Image data
+ * @param string $filename File name (for guessing the type via the extension)
+ * @param string $default Default MIME type
+ * @return string MIME type
+ * @throws \Exception
+ */
+ public static function getMimeTypeByData(string $image_data, string $filename = '', string $default = ''): string
+ {
+ if (substr($default, 0, 6) == 'image/') {
+ Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $default]);
+ return $default;
+ }
+
+ $image = @getimagesizefromstring($image_data);
+ if (!empty($image['mime'])) {
+ Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $default, 'mime' => $image['mime']]);
+ return $image['mime'];
+ }
+
+ return self::guessTypeByExtension($filename);
+ }
+
+ /**
+ * Fetch image mimetype from the image data or guessing from the file name