You have basically two options:
- Trust metadata
- Enter the image source directly.
Option 1:
Probably a faster way, because it only includes a separator string, but may be incorrect. Sort of:
$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA.'; $pos = strpos($data, ';'); $type = explode(':', substr($data, 0, $pos))[1];
Option 2:
Use getimagesize() and the equivalent for the string:
$info = getimagesizefromstring(explode(',', base64_decode($data)[1], 2)); // $info['mime']; contains the mimetype
source share