欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php base64如何转换为图片

程序员文章站 2022-03-06 22:30:34
...

php base64转换为图片的方法:首先获取base64文件;然后设置生成的图片名字;接着通过if语句判断是否有逗号;最后设置图片保存路径即可。

php base64如何转换为图片

推荐:《PHP视频教程

  • 获取base64文件
 $image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASIAAAEiCAYAAABdvt+2AAAgAElEQVR4Xuy9B5hlV3Wm/Z58bq6cujpVB3WUWlJ308oERSQMCAVAAkQQYoi2sQcMAzOD5zcG29hjDGYMxhiTDMaYKEAgwMoSKNBqda7uyunmcHL4n32qW0gCLIG6JAR3P0+pq0q39rl37X2+s/Za3/qWJCPHtMdTagFJkhavJ0VIyEgYyKpPV4/MpReexHXXvIWgZOEcvofueBZfMnDVHKphkpNc/OYUsmoQGiNY6hBKvg8zckBT0TWJrGZTntyPPL2HrD9DKpjDDz3cWCGt5Ik9CJQ+vNwaAnccPfIJl51Oo1rk0r/4AVXPe0rt0b5Y2wJSG4iehk1wDIcgRkJB1TQkxWHD5m4uf94Ozn/OiykdGSdd2UdnNIGvaIRqFl2VMJwSUbOCanYQ6P045iCe2omiRchxgBR6FNI6chhAsURU2osZTeEFDYIgIKeoOIHKQtBJ/qRzCcsLxNWHmA41urqHueDP76UV2E+DUdqXfLIWEA+4OI7RdR3f95PvnymjDURPw0rJsnzsqjFxLKOqEh09Er93+U7eeMVL6S4s574ffp+eYJRccBSXApqZwZQDKB3BDH3cWMcz0rhGL5E5hKNk6TY1gnoVzw3RUgVyikdUOoDcGscNbILQJq20kMOIVqzS1LoYGH4W8/tuoaqayGqBF/79OE7oPg1WaV/yyVpAURTCMCSTyeA4TvL9M2W0gehpWKmfAVFyPkNWInoHJS58/hbe8KLX0JHtoXjoIbTSA2T9cZphD6lUGt1vIZcOYhLixDK1KEbK9RLqgyidG4hbDfKyjOdGeLGKHM+ScorkZYemY9O0HYyoQY46KUOi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2MbBZQUhlCv4niFWksjGI1q1zz//aDomEFLrEUkfCd4mdG9uUpXs725U6QBdpAdI";
  • 设置生成的图片名字
 $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';
  • 判断是否有逗号 如果有就截取后半部分
if (strstr($image,",")){    $image = explode(',',$image);    $image = $image[1];
}
  • 设置图片保存路径
$path = "tmp/signImage/".date("Ymd",time());
  • 判断目录是否存在 不存在就创建 并赋予777权限
if (!is_dir($path)){ //判断目录是否存在 不存在就创建
   mkdir($path,0777,true);
}
  • 拼接路径和图片名称
 $imageSrc=  $path."/". $imageName;
  • 生成图片 返回的是字节数
 $r = file_put_contents(ROOT_PATH ."public/".$imageSrc, base64_decode($image));
  • 判断图片是否生成成功
if (!$r) {   return json(['data'=>null,"code"=>1,"msg"=>"图片生成失败"]);
 }else{   return json(['data'=>1,"code"=>0,"msg"=>"图片生成成功"]);
}
  • 完整代码如下
$image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASIAAAEiCAYAAABdvt+2AAAgAElEQVR4Xuy9B5hlV3Wm/Z58bq6cujpVB3WUWlJ308oERSQMCAVAAkQQYoi2sQcMAzOD5zcG29hjDGYMxhiTDMaYKEAgwMoSKNBqda7uyunmcHL4n32qW0gCLIG6JAR3P0+pq0q39rl37X2+s/Za3/qWJCPHtMdTagFJkhavJ0VIyEgYyKpPV4/MpReexHXXvIWgZOEcvofueBZfMnDVHKphkpNc/OYUsmoQGiNY6hBKvg8zckBT0TWJrGZTntyPPL2HrD9DKpjDDz3cWCGt5Ik9CJQ+vNwaAnccPfIJl51Oo1rk0r/4AVXPe0rt0b5Y2wJSG4iehk1wDIcgRkJB1TQkxWHD5m4uf94Ozn/OiykdGSdd2UdnNIGvaIRqFl2VMJwSUbOCanYQ6P045iCe2omiRchxgBR6FNI6chhAsURU2osZTeEFDYIgIKeoOIHKQtBJ/qRzCcsLxNWHmA41urqHueDP76UV2E+DUdqXfLIWEA+4OI7RdR3f95PvnymjDURPw0rJsnzsqjFx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HsZotlvcNIQURuqbTkjTsZoSu6sgEzM9MsywXML/7y/SYNrqWQyNPJQqQC3k6st3YFYv0yo0444d48Ac3o8czbDxJI230Y+kp6FqDLQ8hlYvENNDieeZrJW7a2+Tvvj9PGIfExCAdSwE/czLBj7sG7Rf85lmgDURP8ZqoqprwPDzPIwgjzFTEBZeu59WvfjWnbj2f+pEigR9jtXxkySBf6KTieklK9jiINVs2mbDG0ds+Q3P0Doa7c6jZVSjZAqGqM3Fkkq0jw1jpEeLaGD/8/Be58oJV+P4RAn0VVaWXMD2MbBZQUhlCv4niFWksjGI1q1zz//aDomEFLrEUkfCd4mdG9uUpXs725U6QBdpAdI";            $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png';            if (strstr($image,",")){                $image = explode(',',$image);                $image = $image[1];
            }            $path = "tmp/signImage/".date("Ymd",time());            if (!is_dir($path)){ //判断目录是否存在 不存在就创建
                mkdir($path,0777,true);
            }            $imageSrc=  $path."/". $imageName;  //图片名字

            $r = file_put_contents(ROOT_PATH ."public/".$imageSrc, base64_decode($image));//返回的是字节数
            if (!$r) {                return json(['data'=>null,"code"=>1,"msg"=>"图片生成失败"]);
            }else{                return json(['data'=>1,"code"=>0,"msg"=>"图片生成成功"]);
            }

以上就是php base64如何转换为图片的详细内容,更多请关注其它相关文章!

相关标签: PHP base64