Search code examples
phpdata-uri

Pull a mime type out of a URI using PHP


So after searching through multiple documentation sources I'm still no closer to figuring out how to extract the Mime type from a data URI that has already been processed and stored in a DB.

image of URI

That is a quick snap shot of the exact data I have to work with. I just want a dynamic way to always get the "image/png" part which may change with each image in the DB.

I'm using PHP.


Solution

  • Not an elegant solution, but you could do:

    // assume you've set $image_uri to be the URI from the database
    $image_parts = explode(";", $image_uri); // split on the ; after the mime type
    $mime_type = substr($image_parts[0], 5); // get the information after the data: text
    

    It could be done with regular expressions, but I'm not good enough at them to come up with it.