PHP后台接收Base64图片

PHP后台接收Base64图片的时候,如何将该字符串转成图片呢?

使用base64_decode 和 fopen和fwrite组合的方法,将图片成功还原;

        //$img为传入字符串
        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);

        $imgPath="test.png";
        if(@file_exists($imgPath)){
            @unlink($imgPath);
        }@clearstatcache();
        $fp=fopen($imgPath,'w');
        fwrite($fp,$data);
        fclose($fp);

 

posted @ 2017-02-26 12:27  益达915  阅读(6548)  评论(0编辑  收藏  举报