如何使用PHP进行base64编码图片处理?

更新于
2026-09-24 20:40:36
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何使用PHP进行base64编码图片处理?

python处理base64编码格式的图片@param $base64_image_content@return mixedfunction saveBase64Image($base64_image_content) { if (preg_match('/^data:image\/(\w+);base64,/', $base64_image_content, $result)) { // 获取图片类型 $image_type=$result[1]; // 移除base64编码前的部分 $image_data=substr($base64_image_content, strpos($base64_image_content, ',') + 1); // 将base64编码转换为二进制数据 $image_data=base64_decode($image_data); // 保存图片到服务器 $image_path='path/to/save/image.' . $image_type; if (file_put_contents($image_path, $image_data)) { return $image_path; } } return false;}

如何使用PHP进行base64编码图片处理?

gistfile1.txt

/** * 处理base64编码格式的图片 * @param $base64_image_content * @return mixed */ function saveBase64Image($base64_image_content){ if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ //图片后缀 $type = $result[2]; //保存位置--图片名 $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type; $path= 'Uploads/image/'; $this->mkdirs($path); $image_url = $path.uniqid().$image_name; $this->mkdirs($image_url); //解码 $decode=base64_decode(str_replace($result[1], '', $base64_image_content)); if (file_put_contents($image_url, $decode)){ $data['code']=0; $data['imageName']=$image_name; $data['url']=$image_url; $data['msg']='保存成功!'; }else{ $data['code']=1; $data['imageName']=''; $data['url']=''; $data['msg']='图片保存失败!'; } }else{ $data['code']=1; $data['imageName']=''; $data['url']=''; $data['msg']='base64图片格式有误!'; } return $data; } //判断目录是否存在 不存在则创建 function mkdirs($dir, $mode = 0777) { if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE; if (!$this->mkdirs(dirname($dir), $mode)) return FALSE; return @mkdir($dir, $mode); }

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

如何使用PHP进行base64编码图片处理?

python处理base64编码格式的图片@param $base64_image_content@return mixedfunction saveBase64Image($base64_image_content) { if (preg_match('/^data:image\/(\w+);base64,/', $base64_image_content, $result)) { // 获取图片类型 $image_type=$result[1]; // 移除base64编码前的部分 $image_data=substr($base64_image_content, strpos($base64_image_content, ',') + 1); // 将base64编码转换为二进制数据 $image_data=base64_decode($image_data); // 保存图片到服务器 $image_path='path/to/save/image.' . $image_type; if (file_put_contents($image_path, $image_data)) { return $image_path; } } return false;}

如何使用PHP进行base64编码图片处理?

gistfile1.txt

/** * 处理base64编码格式的图片 * @param $base64_image_content * @return mixed */ function saveBase64Image($base64_image_content){ if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ //图片后缀 $type = $result[2]; //保存位置--图片名 $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type; $path= 'Uploads/image/'; $this->mkdirs($path); $image_url = $path.uniqid().$image_name; $this->mkdirs($image_url); //解码 $decode=base64_decode(str_replace($result[1], '', $base64_image_content)); if (file_put_contents($image_url, $decode)){ $data['code']=0; $data['imageName']=$image_name; $data['url']=$image_url; $data['msg']='保存成功!'; }else{ $data['code']=1; $data['imageName']=''; $data['url']=''; $data['msg']='图片保存失败!'; } }else{ $data['code']=1; $data['imageName']=''; $data['url']=''; $data['msg']='base64图片格式有误!'; } return $data; } //判断目录是否存在 不存在则创建 function mkdirs($dir, $mode = 0777) { if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE; if (!$this->mkdirs(dirname($dir), $mode)) return FALSE; return @mkdir($dir, $mode); }