PHP后台如何处理base64图片

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

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

 //文件夹路径
        $destination_folder = "../attachment/" . $_GPC['m'] . "/" . date('Ymd') . "/"; //上传文件路径
        //如果不存在文件夹就创建
        if (!file_exists($destination_folder)) {
            mkdir($destination_folder);
        }
        //创建生成图片的路径
        $destination = $destination_folder . str_shuffle(time() . rand(111111, 999999)) . "." . "png";
        $pinfo = pathinfo($destination);
        $fname = $pinfo['basename'];

        //echo $fname;
        @require_once(IA_ROOT . '/framework/function/file.func.php');
        @$filename = $fname;
        @file_remote_upload($filename);
        //接收到前端传过来的base64格式的图片
        $img="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCAAcAHQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDz+t7SfDkt7bpPIDslPyAHk1hxDMqD1YV694bt0TA4PlIFXHT8KAMq38EW4gVWtkJ65Zjk1z+t+FRZE+UrqT0BOQPxr1Ws/WYUlsHLgHbyM0AeIyI0blHBBHY02vRLzQtPvtrvBOrdyneqv/CKaZ/cu/zoA4Wiu7/4RPTP+ed3+dH/AAiemf8APO7/ADoA4Siu7/4RPTP+ed3+dH/CJ6Z/zzu/zoA5LStPN/MRnCL1rvdJ8MRi1TbaoSP+WknJP4U/TdMtLSSG3hhZQW5Zxya7FQFAAGAKAOH1HwnbLbt51tzztljONp+nSuE1OyayuTHtbZ/CT3r3J1DqVYZBGCK8u8cRhDCAv3XYZoA5KiiigBVOGB9DXo/hjXUe3jlbarKoSRAew6GvN6fFNJCcxuVz6UAe5pqFq6bhMuPesHxFr0KQmKJuOrNXmP8AaV6f+XmT86jlu55gRLKzZ9TQBo6l4gv7q73x3c0cacIqOVA/Kq6a1qS7v9NnORjlycVQooA0DrmqHGb+c44GWoGuaopyt/OPo9Z9FAF/+29T/wCf6b/vqj+29T/5/pv++qoUUAdd4f1+4lKxXMu9ozlWY8mvQrPVbe4iUs4Rscg14ijsjBkJBHcVvaZrN3GkaEq6jswPP60Aen3uqwQQt5bb3xxjtXmPivUxdvHbKCPLYsx9SaXU9dvZIZIgyRoeyLg/n1rn3dnYs5JJ7mgBtFFFAH//2Q==";
        //剔除头部信息,得到Base64位码
        $img = str_replace('data:image/jpeg;base64,', '', $img);
        //进行解析成普通图片流
        $data = base64_decode($img);

        if(@file_exists($destination)){
            @unlink($destination);
        }@clearstatcache();
        //打开文件流
        $fp=fopen($destination,'w');
        //写到新的图片中
        fwrite($fp,$data);
        fclose($fp);
        //图片处理完毕

文章来源: PHP后台如何处理base64图片

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:PHP后台如何处理base64图片