php接收base64数据生成图片并保存

来源:博客园 分类: 文章浏览史 发布时间:2020-10-21 19:53:52 最后更新:2020-10-21 浏览:819
转载声明:
本文为摘录自“博客园”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-10-21 19:53:52
复制代码
    public function base64(){
        //接收base64数据
        $image= $_POST['imegse'];
        //设置图片名称
        $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
        //判断是否有逗号 如果有就截取后半部分
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }
        //设置图片保存路径
        $path = "./".date("Ymd",time());

        //判断目录是否存在 不存在就创建
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }

        //图片路径
        $imageSrc= $path."/". $imageName;

        //生成文件夹和图片
        $r = file_put_contents($imageSrc, base64_decode($image));
        if (!$r) {
            return json(['code'=>0,'message'=>'图片生成失败']);
        }else {
            return json(['code'=>1,'message'=>'图片生成成功']);
        }
}
复制代码

 

php技术微信