php处理base64格式图片上传

方法

$base64_image_content:要保存的Base64
$path:要保存的路径
public function base64_image_content($base64_image_content,$path){
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            //$ff=date('Y-m-d',time());
            $new_file = $path."/lunbo/";
            if(!file_exists($new_file)){
                //检查是否有该文件夹,如果没有就创建,并给予最高权限
                mkdir($new_file, 0700);
            }
            $picname=mt_rand(0,99).time().".{$type}";
            $new_file = $new_file.$picname;
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                return "/Uploads/lunbo/".$picname;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

调用

$data['pic'] = $this->base64_image_content($_POST['thumb_url'],'./Uploads');

你可能感兴趣的:(php处理base64格式图片上传)