如何对base64编码的图片进行有效处理?

更新于
2026-09-24 20:13:18
1阅读来源:SEO教程
  • 内容介绍
  • 相关推荐

本文共计150个文字,预计阅读时间需要1分钟。

如何对base64编码的图片进行有效处理?

使用base64格式处理图片的功能如下:

phpfunction save_img_by_base($base64_image_content) { header('Content-type:text/; charset=utf-8'); // 将base64字符串保存为图片 if (preg_match('/^data:image\/(\w+);base64,([^\s]*)$/i', $base64_image_content, $result)) { // 获取图片类型 $image_type=$result[1]; // 获取base64编码的图片数据 $image_data=$result[2]; // 解码base64字符串 $image_data=base64_decode($image_data); // 设置保存路径 $save_path='path/to/save/image.jpg'; // 保存图片 file_put_contents($save_path, $image_data); }}

base64格式的图片处理

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; } } }

如何对base64编码的图片进行有效处理?

本文共计150个文字,预计阅读时间需要1分钟。

如何对base64编码的图片进行有效处理?

使用base64格式处理图片的功能如下:

phpfunction save_img_by_base($base64_image_content) { header('Content-type:text/; charset=utf-8'); // 将base64字符串保存为图片 if (preg_match('/^data:image\/(\w+);base64,([^\s]*)$/i', $base64_image_content, $result)) { // 获取图片类型 $image_type=$result[1]; // 获取base64编码的图片数据 $image_data=$result[2]; // 解码base64字符串 $image_data=base64_decode($image_data); // 设置保存路径 $save_path='path/to/save/image.jpg'; // 保存图片 file_put_contents($save_path, $image_data); }}

base64格式的图片处理

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; } } }

如何对base64编码的图片进行有效处理?