温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中base64转换为图片的方法

发布时间:2020-08-31 09:56:18 来源:亿速云 阅读:179 作者:小新 栏目:编程语言

小编给大家分享一下php中base64转换为图片的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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

php中base64转换为图片的方法

  • 获取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/yyVpAURTCMCSTyeA4TvL9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zPIwgjzFTEBZeu59WvfjWnbj2f+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());
  • 判断目录是否存在 不存在就创建 并赋予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/OiykdGSdd2UdnNIGvaIRqFl2VMJwSUbOC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pqLhGCkK+R5kx6dhTZKxF0gHVcbHpxlYtRrbS5HvLBAiY9khWqtBINnE8RyjlYD3fHWeA5U2ED3eGrb//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转换为图片的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI