上传base64格式的图片到服务器

上传base64格式的图片到服务器

/**bash64上传图片 
         * @param $base64 图片的base64数据 
         * @param $path 保存路径 
         */  
        function base64_upload($base64, $path)  
        {  
            //POST过程中(加号会被替换成空格)  
            $base64_image = str_replace(' ', '+', $base64);  
  
            if (preg_match('/^(data:s*image/(w+);base64,)/', $base64_image, $result)) {  
  
                //所有图片后缀改成jpg  
                if ($result[2] != 'jpg') {  
                    $image_name = uniqid() . '.jpg';  
                }  
  
                $image_file = $path . $image_name;  
  
                //保存图片  
                if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))) {  
                    return $image_name;  
                } else {  
                    return false;  
                }  
            } else {  
                return false;  
            }  
        }  
  
        $path = './Public/Upload/';  
        //调用方法  
        $result = base64_upload($base64_img, $path);  
        //返回图片的名称  
        print_r($result);  
原文地址:https://www.cnblogs.com/jacko/p/6306094.html