I'm storing small images in database (MySQL, blob
), so when I retrieve any image it's in variable.
My plan is to display image with CSS as base64 encoded in background image: background: url(data:image/…
.
To perform that I need to detect MIME type of image which is in variable. There's exif_imagetype, but it works only with files.
Is there any way to detect MIME type of image which isn't stored as file, but in variable?
You can use getimagesizefromstring
to get information about an image blob, including the image’s MIME type. It is available since PHP 5.4.
However, I would also suggest to store the MIME type in the database. Heuristic approaches are “expensive” and storing a few bytes in the database is much more efficient than guessing the image type over and over again.