如何将base64编码的图片保存至本地并获取保存路径?
- 内容介绍
- 文章标签
- 相关推荐
本文共计126个文字,预计阅读时间需要1分钟。
将伪原创内容改写如下:
将base64编码的图片保存到本地并返回路径.txtjavascriptfunction save_img_by_base($base64_image_content) { header('Content-type:text/; charset=utf-8'); // 将base64字符串保存为图片 if (preg_match('/^data:image\/(\w+);base64,/', $base64_image_content)) { // ... }}
function save_img_by_base($base64_image_content){ header('Content-type:text/html;charset=utf-8'); //保存base64字符串为图片 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2];//匹配出图片的格式 $root_path= dirname(ROOT_PATH)."/images/cardarticleimg/"; //图片保存的路径 $path=date('Y-m-d').'/'; $mulu=$root_path.$path; if(!file_exists($mulu)){ mkdir($mulu,0777,true); chmod($mulu,0777); } $file_name = mt_rand().".$type"; $new_file=$mulu.$file_name; /*$new_file = "./test.{$type}";*/ if (file_put_contents("$new_file", base64_decode(str_replace($result[1], '', $base64_image_content)))){ return $path.$file_name; } } }
本文共计126个文字,预计阅读时间需要1分钟。
将伪原创内容改写如下:
将base64编码的图片保存到本地并返回路径.txtjavascriptfunction save_img_by_base($base64_image_content) { header('Content-type:text/; charset=utf-8'); // 将base64字符串保存为图片 if (preg_match('/^data:image\/(\w+);base64,/', $base64_image_content)) { // ... }}
function save_img_by_base($base64_image_content){ header('Content-type:text/html;charset=utf-8'); //保存base64字符串为图片 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2];//匹配出图片的格式 $root_path= dirname(ROOT_PATH)."/images/cardarticleimg/"; //图片保存的路径 $path=date('Y-m-d').'/'; $mulu=$root_path.$path; if(!file_exists($mulu)){ mkdir($mulu,0777,true); chmod($mulu,0777); } $file_name = mt_rand().".$type"; $new_file=$mulu.$file_name; /*$new_file = "./test.{$type}";*/ if (file_put_contents("$new_file", base64_decode(str_replace($result[1], '', $base64_image_content)))){ return $path.$file_name; } } }

