base64_decode 图片转换文件存储PHP 富文本处理

参考链接:https://blog.csdn.net/u011415782/article/details/79978608


    /**
     * 编辑器base64_decode图片匹配
     *
     * @author Eric
     * @param $str
     * @return mixed
     */
    public function nei_rong_img($str)
    {
        preg_match_all('/data:\S+/',$str,$res);
        $url ="http://mapi.zhinong.com/";

        // var_dump($res);die;
        $res = $res[0];
        if(count($res) > 0)
        {

            foreach ($res as $k => $v)
            {
                $path = $this->images_save(substr($v,0, strlen($v)-1));
                $str = str_replace(
                    $v,
                    $url.$path.'"',
                    $str);
            }
        }
        return $str;
    }

    /**
     * base64_decode图片存储到目录
     *
     * @author Eric
     * @param $imgBase64
     * @return string
     */
    public function images_save($imgBase64)
    {
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$imgBase64,$res)) {
            //获取图片类型
            $type = $res[2];

            //图片保存路径
            $new_file = __ROOT__ . 'Application/img/'.date("Ymd",time());




            if (!file_exists($new_file)) {

             $res=mkdir($new_file,0777,true);

            }

            $new_file = $new_file.time().'.'.$type;
            file_put_contents($new_file,base64_decode(str_replace($res[1],'', $imgBase64)));
            return $new_file;
        }
    }

你可能感兴趣的:(base64_decode 图片转换文件存储PHP 富文本处理)