php中base64转换为图片的方法-创新互联
小编给大家分享一下php中base64转换为图片的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联建站主营果洛州网站建设的网络公司,主营网站建设方案,App定制开发,果洛州h5小程序定制开发搭建,果洛州网站营销推广欢迎果洛州等地区企业咨询php base64转换为图片的方法:首先获取base64文件;然后设置生成的图片名字;接着通过if语句判断是否有逗号;最后设置图片保存路径即可。
- 获取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/hbe8KLX0JHtoXjoIbTSA2T9cZ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2mbDG0ds+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());
- 判断目录是否存在 不存在就创建 并赋予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/qRzCcsLxNWHmA41ur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ED3eGrb//4m1QBuITqw9H3c2kWL9GRAZZPMSp+8aYPPWVbz1zX/E/jvHsZotlvcNIQURuqbTkjTsZoSu6sgEzM9MsywXML/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转换为图片的方法-创新互联
转载来于:http://azwzsj.com/article/cddceh.html