'Get image type from base64 encoded src string

WHAT I REQUIRE

My image src looks like this

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...

How can I extract the image type ie; jpeg from the above src given. I am using PHP and the image type can also be png/gif/jpg.



Solution 1:[1]

Well you have basically two options:

  1. Trust the metadata
  2. Type check the image source directly

Option 1:

Probably the quicker way because it only involve splitting string, but it may be incorrect. Something like:

$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA.';
$pos  = strpos($data, ';');
$type = explode(':', substr($data, 0, $pos))[1];

Option 2:

Use getimagesize() and it's equivalent for string:

$info = getimagesizefromstring(explode(',', base64_decode($data)[1], 2));
// $info['mime']; contains the mimetype

Solution 2:[2]

Test this:

<?php

$str = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';

function getB64Type($str) {
    // $str should start with 'data:' (= 5 characters long!)
    return substr($str, 5, strpos($str, ';')-5);
}
var_dump(getB64Type($str));

Solution 3:[3]

This is the way that i made:

$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF......."
$img = explode(',', $uri);
$ini =substr($img[0], 11);
$type = explode(';', $ini);
echo $type[0]; // result png

Solution 4:[4]

I hope this helps but the correct way to do this is.

$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF......."
$encodedImgString = explode(',', $uri, 2)[1];
$decodedImgString = base64_decode($encodedImgString);
$info = getimagesizefromstring($decodedImgString);

echo $info['mime'];

Please don't just use the, data:image/png as that is not reliable, I could easily fake that part and send you a base64 encoded .exe file.

Solution 5:[5]

$encoded_string = "....";
$imgdata = base64_decode($encoded_string);

$f = finfo_open();

$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);    

Solution 6:[6]

You can use this, if you use ajax to upload images then you will not get the correct MIME using finfo_buffer

function getMIMETYPE($base64string){
    preg_match("/^data:(.*);base64/g",$base64string, $match);
    echo $match[1];
}

Solution 7:[7]

$str64 = base64 string

function base64Extension($str64) {
  return explode(";", explode("/", $str64)[1])[0];
}

Solution 8:[8]

Use below regex code, it returns the extension of a file.

$base64string = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
preg_match("/^data:image\/(.*);base64/i",$base64string, $match);
$extension = $match[1];

Solution 9:[9]

$str = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
$ext = end(explode('/', (explode(';', $str))[0]));

Result: jpeg

Solution 10:[10]

This solution worked best for me, piggybacking off of Option 1 presented by Boris Guery.

$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
$pos  = strpos($data, ';');
$type = explode('/', substr($data, 0, $pos))[1];

In this instance the solution returns jpeg file extension, in the $type variable.

Solution 11:[11]

String[] strings = base64String.split(",");
String extension;
switch (strings[0]) {//check image's extension
    case "data:image/jpeg;base64":
        extension = "jpeg";
        break;
    case "data:image/png;base64":
        extension = "png";
        break;
    default://should write cases for more images types
        extension = "jpg";
        break;
}

Solution 12:[12]

$type will return "data:image/jpeg"

then $extension returns "jpeg"

  $type      = explode(';', $httpFileRequest)[0];
  $extension = explode('/', $type)[1];

Solution 13:[13]

This is how I do:

$string = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
// $string = 'data:video/mp4;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';

function getMimeType($string)
{
    $string = explode(
        ';base64,',
        stristr($string, ';base64,', true)
    );

    if(empty($string[0])){
        return false;
    }

    preg_match('/^data:(.*)\/(.*)/', $string[0], $match);

    return [
        'type' => $match[1],
        'extension' => $match[2],
    ];
}

var_dump(
    getMimeType($string)
);
// array(2) { ["type"]=> string(5) "image" ["extension"]=> string(4) "jpeg" }
// array(2) { ["type"]=> string(5) "video" ["extension"]=> string(3) "mp4" }

Solution 14:[14]

Best Way to get base64 image file type

    $type = explode('/', mime_content_type($base64_string))[1];

Solution 15:[15]

It's works for me and it's the best way to get base64 image file type. it only returns file extenstion like 'png', 'jpg', etc

$photo = data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...
$ext = explode('/',explode(':',substr($photo,0,strpos($photo,';')))[1])[1];

return : jpeg

Solution 16:[16]

This is best solution worked for me...

$str = "data:image/jpg;base64,R0lGODlhPQBEAPeoAJow==";
$name = time() . '.' . explode('/', explode(':', substr($str, 0, strpos(str, ';')))[1])[1];

Result: 1564650041.jpg