如何从PHP中的base 64字符串获取图像大小

Ran*_*hir 4 php base64 file-upload

我正在为图像的字符串获取base 64,我必须将其移动到文件夹中并将图像路径存储在数据库中,但必须限制文件大小。我该如何做:

我的从base 64字符串生成图像的代码如下:

/** if image is attached with request **/
 $Image = "MyBase64StringHere";
 list($type, $Image) = explode(';', $Image);
 list(, $Image)      = explode(',', $Image);

/** decode the base 64 image **/
 $Image = base64_decode($Image);

I have tried to get image size like:
$size =  getimagesize($Image);
Run Code Online (Sandbox Code Playgroud)

但是从中获取文件的宽度和高度,有人可以告诉我如何从中获取文件的大小。

Ja͢*_*͢ck 8

getimagesize()函数也应该只适用于 Data URI:

$Image = "MyBase64StringHere";
$size = getimagesize($Image);
Run Code Online (Sandbox Code Playgroud)


pra*_*ava 6

请检查:

解码后,请尝试使用:getimagesizefromstring(),如果您使用PHP 5.4,如果使用PHP 5.3,则可以使用以下方法进行检查。

<?php
if (!function_exists('getimagesizefromstring')) {
    function getimagesizefromstring($data)
    {
        $uri = 'data://application/octet-stream;base64,' . base64_encode($data);
        return getimagesize($uri);
    }
}
Run Code Online (Sandbox Code Playgroud)