简体   繁体   English

从 base64 编码的 src 字符串中获取图像类型

[英]Get image type from base64 encoded src string

WHAT I REQUIRE我的要求

My image src looks like this我的图像 src 看起来像这样

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

How can I extract the image type ie;如何提取图像类型,即; jpeg from the above src given. jpeg来自上面给出的 src。 I am using PHP and the image type cacn also be png and gif.我正在使用 PHP,图像类型 cacn 也是 png 和 gif。

Well you have basically two options:那么你基本上有两个选择:

  1. Trust the metadata信任元数据
  2. Type check the image source directly直接打字查看图片来源

Option 1:选项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:选项 2:

Use getimagesize() and it's equivalent for string:使用getimagesize()并且它等效于字符串:

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

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));

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

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.请不要只使用data:image/png ,因为它不可靠,我可以轻松伪造该部分并向您发送 base64 编码的 .exe 文件。

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

$f = finfo_open();

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

You can use this, if you use ajax to upload images then you will not get the correct MIME using finfo_buffer您可以使用它,如果您使用 ajax 上传图像,那么您将无法使用 finfo_buffer 获得正确的 MIME

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

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

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];
$str = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA...';
$ext = end(explode('/', (explode(';', $str))[0]));

Result: jpeg结果:jpeg

This solution worked best for me, piggybacking off of Option 1 presented by Boris Guery.这个解决方案最适合我,搭载了 Boris Guery 提出的选项 1。

$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.在这种情况下,解决方案在$type变量中返回jpeg文件扩展名。

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;
}

$type will return "data:image/jpeg" $type 将返回 "data:image/jpeg"

then $extension returns "jpeg"然后 $extension 返回“jpeg”

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

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" }

获取 base64 图像文件类型的最佳方法

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

It's works for me and it's the best way to get base64 image file type.它对我有用,它是获取 base64 图像文件类型的最佳方法。 it only returns file extenstion like 'png', 'jpg', etc它只返回文件扩展名,如“png”、“jpg”等

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

return : jpeg返回: jpeg

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM