Detecting image type from base64 string in PHP

前端 未结 7 1460
闹比i
闹比i 2020-11-27 14:32

Is it possible to find out the type of an image encoded as a base64 String in PHP?

I have no method of accessing the original image file, just the encoded string. F

7条回答
  •  醉话见心
    2020-11-27 14:59

    The solution given by @Marc B is the best one for me (if our php version is > 5.3.0 otherwise we can use the solution given by @Aaron Murgatroyd).

    I would like to give a little addition to this solution.

    To get the image type you can do it like this :

    $split = explode( '/', $mime_type );
    $type = $split[1]; 
    

    In fact, (if you don't know it) the mime type for images is : image/type and type can be png or gif or jpeg or ...

    Hope that can help someone and thanks to @Marc B for his solution.

    For an exhaustive list of mime type you can look here :

    • http://www.sitepoint.com/web-foundations/mime-types-complete-list/

提交回复
热议问题