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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18658437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:58:45  来源:igfitidea点击:

Get image type from base64 encoded src string

phpimage

提问by Developer

WHAT I REQUIRE

我的要求

My image src looks like this

我的图像 src 看起来像这样

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

How can I extract the image type ie; jpegfrom the above src given. I am using PHP and the image type cacn also be png and gif.

如何提取图像类型,即;jpeg从上面给出的src。我正在使用 PHP,图像类型 cacn 也是 png 和 gif。

回答by Boris Guéry

Well you have basically two options:

那么你基本上有两个选择:

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

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

回答by ComFreek

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

回答by Tiago Mendes

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

回答by Juanes Rios

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

$f = finfo_open();

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

回答by Matthew A Thomas

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/pngas that is not reliable, I could easily fake that part and send you a base64 encoded .exe file.

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

回答by imal hasaranga perera

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

回答by suman

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

回答by Adam Pery

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

Result: jpeg

结果:jpeg

回答by Keith Gulbro

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 jpegfile extension, in the $typevariable.

在这种情况下,解决方案jpeg$type变量中返回文件扩展名。

回答by Aymen Bouein

$str64 = base64 string

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