php通过图片资源上传图片

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_39616995/article/details/98480565
// $base64  图片的base64资源  
// $path    上传的路径
function uploadimg($base64,$path)
{
 // 解决传参时乱码的问题
    $base64 = str_replace(' ','+',$base64); 
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
       //图片的类型 jpg png等
        $type = $result[2];
        //重命名
        $new_file = $path.time().".{$type}";
        //资源写入文件
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){
            return '/'.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39616995/article/details/98480565