Is it possible to find out the type of an image encoded as a base64 String in PHP?
是否有可能找出在PHP中编码为base64字符串的图像类型?
I have no method of accessing the original image file, just the encoded string. From what I've seen, imagecreatefromstring()
can create an image resource from a string representation (after it's been decoded from base64), but it automatically detects the image type and the image resource itself is a special PHP representation. In case I want to save the image as a file again, I would have no idea if the type I am saving it as corresponds to the original type from which the String representation was created.
我没有访问原始图像文件的方法,只有编码的字符串。从我所看到的,imagecreatefromstring()可以从字符串表示(从base64解码后)创建图像资源,但它会自动检测图像类型,图像资源本身是一个特殊的PHP表示。如果我想再次将图像保存为文件,我不知道我保存的类型是否与创建字符串表示的原始类型相对应。
5 个解决方案
#1
78
FileInfo can do that for you:
FileInfo可以为您做到这一点:
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
#2
33
If you dont want to use these functions because of their dependencies you can use the first bytes of the data:
如果由于它们的依赖性而不想使用这些函数,则可以使用数据的第一个字节:
function getBytesFromHexString($hexdata)
{
for($count = 0; $count < strlen($hexdata); $count+=2)
$bytes[] = chr(hexdec(substr($hexdata, $count, 2)));
return implode($bytes);
}
function getImageMimeType($imagedata)
{
$imagemimetypes = array(
"jpeg" => "FFD8",
"png" => "89504E470D0A1A0A",
"gif" => "474946",
"bmp" => "424D",
"tiff" => "4949",
"tiff" => "4D4D"
);
foreach ($imagemimetypes as $mime => $hexbytes)
{
$bytes = getBytesFromHexString($hexbytes);
if (substr($imagedata, 0, strlen($bytes)) == $bytes)
return $mime;
}
return NULL;
}
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$mimetype = getImageMimeType($imgdata);
#3
6
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).
@Marc B给出的解决方案对我来说是最好的(如果我们的php版本> 5.3.0,否则我们可以使用@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 ...
事实上,(如果你不知道)图像的mime类型是:image / type和type可以是png或gif或jpeg或者......
Hope that can help someone and thanks to @Marc B for his solution.
希望能帮到某人并感谢@Marc B的解决方案。
For an exhaustive list of mime type you can look here :
有关mime类型的详尽列表,您可以在此处查看:
- http://www.sitepoint.com/web-foundations/mime-types-complete-list/
- http://www.sitepoint.com/web-foundations/mime-types-complete-list/
#4
3
The way shown by @Marc B is the nicest.
@Marc B显示的方式是最好的。
Should FInfo
not be available, the only other way I know is to store the data into a file, and run a getimagesize()
on it.
如果FInfo不可用,我知道的另一种方法是将数据存储到文件中,并在其上运行getimagesize()。
#5
2
If you know a minimal amount about the file format structure, you could theoretically look at the top bytes of the file until you could work out what type of file it is.
如果您知道文件格式结构的最小数量,理论上您可以查看文件的顶部字节,直到您可以确定它是什么类型的文件。
For example, a GIF image always starts with the following bytes GIF89a
. If you can find that string at the begining of the file, you can be reasonably sure that it is a GIF image and absolutely certain it isn't any other image format. (it could still be a text file though, that just happens to start with 'GIF89a'; you'd have to parse more of the file to be absolutely certain)
例如,GIF图像始终以以下字节GIF89a开头。如果你可以在文件的开头找到该字符串,你可以合理地确定它是一个GIF图像,并且绝对肯定它不是任何其他图像格式。 (它可能仍然是一个文本文件,恰好以'GIF89a'开头;你必须解析更多的文件才能绝对确定)
Likewise, PNG files have the string PNG
fairly near the start (it's not quite at the very begining; again, you'd need to research the file format specifics to help you determine how much you'd need to know to be certain).
同样地,PNG文件在开始时非常接近字符串PNG(它不是刚刚开始;再次,您需要研究文件格式细节以帮助您确定需要知道多少才能确定)。
JPEGs also contain recognisable strings in their headers, although these are more varied and complex. You might want to look out for the string Exif
.
JPEG在标题中也包含可识别的字符串,尽管这些字符串更加多样和复杂。您可能想要查找字符串Exif。
Getting the file format definitions would definitely give you more accuracy, but depending on how accurate you need to be, you might learn enough about the files formats just by opening some image files in a binary editor to see how they're structured.
获取文件格式定义肯定会提供更高的准确性,但根据您需要的准确程度,您可以通过在二进制编辑器中打开一些图像文件来了解文件格式,以了解它们的结构。
These resources may help you:
这些资源可以帮助您:
-
http://www.w3.org/Graphics/JPEG/
http://www.w3.org/Graphics/JPEG/
-
http://www.w3.org/Graphics/PNG/
http://www.w3.org/Graphics/PNG/
#1
78
FileInfo can do that for you:
FileInfo可以为您做到这一点:
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
#2
33
If you dont want to use these functions because of their dependencies you can use the first bytes of the data:
如果由于它们的依赖性而不想使用这些函数,则可以使用数据的第一个字节:
function getBytesFromHexString($hexdata)
{
for($count = 0; $count < strlen($hexdata); $count+=2)
$bytes[] = chr(hexdec(substr($hexdata, $count, 2)));
return implode($bytes);
}
function getImageMimeType($imagedata)
{
$imagemimetypes = array(
"jpeg" => "FFD8",
"png" => "89504E470D0A1A0A",
"gif" => "474946",
"bmp" => "424D",
"tiff" => "4949",
"tiff" => "4D4D"
);
foreach ($imagemimetypes as $mime => $hexbytes)
{
$bytes = getBytesFromHexString($hexbytes);
if (substr($imagedata, 0, strlen($bytes)) == $bytes)
return $mime;
}
return NULL;
}
$encoded_string = "....";
$imgdata = base64_decode($encoded_string);
$mimetype = getImageMimeType($imgdata);
#3
6
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).
@Marc B给出的解决方案对我来说是最好的(如果我们的php版本> 5.3.0,否则我们可以使用@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 ...
事实上,(如果你不知道)图像的mime类型是:image / type和type可以是png或gif或jpeg或者......
Hope that can help someone and thanks to @Marc B for his solution.
希望能帮到某人并感谢@Marc B的解决方案。
For an exhaustive list of mime type you can look here :
有关mime类型的详尽列表,您可以在此处查看:
- http://www.sitepoint.com/web-foundations/mime-types-complete-list/
- http://www.sitepoint.com/web-foundations/mime-types-complete-list/
#4
3
The way shown by @Marc B is the nicest.
@Marc B显示的方式是最好的。
Should FInfo
not be available, the only other way I know is to store the data into a file, and run a getimagesize()
on it.
如果FInfo不可用,我知道的另一种方法是将数据存储到文件中,并在其上运行getimagesize()。
#5
2
If you know a minimal amount about the file format structure, you could theoretically look at the top bytes of the file until you could work out what type of file it is.
如果您知道文件格式结构的最小数量,理论上您可以查看文件的顶部字节,直到您可以确定它是什么类型的文件。
For example, a GIF image always starts with the following bytes GIF89a
. If you can find that string at the begining of the file, you can be reasonably sure that it is a GIF image and absolutely certain it isn't any other image format. (it could still be a text file though, that just happens to start with 'GIF89a'; you'd have to parse more of the file to be absolutely certain)
例如,GIF图像始终以以下字节GIF89a开头。如果你可以在文件的开头找到该字符串,你可以合理地确定它是一个GIF图像,并且绝对肯定它不是任何其他图像格式。 (它可能仍然是一个文本文件,恰好以'GIF89a'开头;你必须解析更多的文件才能绝对确定)
Likewise, PNG files have the string PNG
fairly near the start (it's not quite at the very begining; again, you'd need to research the file format specifics to help you determine how much you'd need to know to be certain).
同样地,PNG文件在开始时非常接近字符串PNG(它不是刚刚开始;再次,您需要研究文件格式细节以帮助您确定需要知道多少才能确定)。
JPEGs also contain recognisable strings in their headers, although these are more varied and complex. You might want to look out for the string Exif
.
JPEG在标题中也包含可识别的字符串,尽管这些字符串更加多样和复杂。您可能想要查找字符串Exif。
Getting the file format definitions would definitely give you more accuracy, but depending on how accurate you need to be, you might learn enough about the files formats just by opening some image files in a binary editor to see how they're structured.
获取文件格式定义肯定会提供更高的准确性,但根据您需要的准确程度,您可以通过在二进制编辑器中打开一些图像文件来了解文件格式,以了解它们的结构。
These resources may help you:
这些资源可以帮助您:
-
http://www.w3.org/Graphics/JPEG/
http://www.w3.org/Graphics/JPEG/
-
http://www.w3.org/Graphics/PNG/
http://www.w3.org/Graphics/PNG/