PHP Snippet 1:
$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA.';
$pos = strpos($data, ';');
$type = explode(':', substr($data, 0, $pos))[1];
PHP Snippet 2:
$info = getimagesizefromstring(explode(',', base64_decode($data)[1], 2));
// $info['mime']; contains the mimetype
PHP Snippet 3:
<?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));
PHP Snippet 4:
$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF......."
$img = explode(',', $uri);
$ini =substr($img[0], 11);
$type = explode(';', $ini);
echo $type[0]; // result png
PHP Snippet 5:
$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAF......."
$encodedImgString = explode(',', $uri, 2)[1];
$decodedImgString = base64_decode($encodedImgString);
$info = getimagesizefromstring($decodedImgString);
echo $info['mime'];
PHP Snippet 6:
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
PHP Snippet 7:
function getMIMETYPE($base64string){
preg_match("/^data:(.*);base64/g",$base64string, $match);
echo $match[1];
}
PHP Snippet 8:
$str64 = base64 string
function base64Extension($str64) {
return explode(";", explode("/", $str64)[1])[0];
}
PHP Snippet 9:
$base64string = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
preg_match("/^data:image\/(.*);base64/i",$base64string, $match);
$extension = $match[1];
PHP Snippet 10:
$str = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
$ext = end(explode('/', (explode(';', $str))[0]));
PHP Snippet 11:
$data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
$pos = strpos($data, ';');
$type = explode('/', substr($data, 0, $pos))[1];
PHP Snippet 12:
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;
}
PHP Snippet 13:
$type = explode(';', $httpFileRequest)[0];
$extension = explode('/', $type)[1];
PHP Snippet 14:
$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" }
PHP Snippet 15:
$type = explode('/', mime_content_type($base64_string))[1];
PHP Snippet 16:
$photo = data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...
$ext = explode('/',explode(':',substr($photo,0,strpos($photo,';')))[1])[1];
PHP Snippet 17:
$str = "data:image/jpg;base64,R0lGODlhPQBEAPeoAJow==";
$name = time() . '.' . explode('/', explode(':', substr($str, 0, strpos(str, ';')))[1])[1];
Result: 1564650041.jpg