美文网首页
网页富文本编辑器如何将默认 base64 图片上传衔接为服务器保

网页富文本编辑器如何将默认 base64 图片上传衔接为服务器保

作者: 爱绑架的猫 | 来源:发表于2021-01-29 10:46 被阅读0次
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Storage;

class ArticleController extends Controller
{
    public function testBaseUpload($pictrue="") {
        $pictrue1 = ''; // 获取图片转base64的字符串
        $result = [];
        // 转化base64编码图片 jpeg、jpg、png、bmp、gif
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $pictrue, $result)) {
            $type = $result[2]; // 获取图片类型
            if(in_array($type, array('pjpeg','jpeg','jpg','bmp','png'))) {
                // 图片名字               
                $fileName = time().rand(1,1000).'.'.$type; // 图片名称
                // 临时文件
                // $tmpfname = tempnam("./image/", "FOO"); // windows
                // $tmpfname = ('/tmp/','FOO'); // linux
                // $tmpfname = tempnam('c:/wamp64/tmp','FOO');
                $tmpfname = tempnam('/home/wwwroot/cme/cme/cmepro/public/tmp/','FOO');
                // 保存图片
                $handle = fopen($tmpfname, "w");
     
                // return [$pic, $result];
                $object = '/'.date('Y/m/d').'/'.$fileName; // 阿里云上传地址
                // $object = APP_DIR.'/'.$fileName;
                
                if (fwrite($handle, base64_decode(str_replace($result[1], '', $pictrue))))
                {
                    // 上传图片至阿里云OSS
                    $data = Storage::disk('oss')->put($object,file_get_contents($tmpfname));
                    // 关闭缓存
                    fclose($handle);
                    // 删除本地该图片
                    unlink($tmpfname);
                    // 返回图片链接
                    // dd($data,env('AliImgUrl').$object);
                    return env('AliImgUrl').$object;
                }else {
                    // return response(['message'=>'图片上传失败'], 200);
                    return false;
                }
     
            }else {
                // return response(['message'=>'图片类型错误'], 200);
                return false;
            }                   
        } else {
            // return response(['message'=>'图片编码错误'], 200);
            return false;
        }
    }

    public function replaceImgUrl($content=1) {
        // $content = '<img src="dadfadsfasd" data-filename="timg (4).jpg" style="width: 884px;"> <br><br><br><br>dafdsfadf 
        //             <img alt="" src="test2.jpg" width="120" height="120">';
        ini_set("pcre.backtrack_limit", "-1");
        ini_set("pcre.recursion_limit", "-1"); 
        
        //提取图片路径的src的正则表达式
        preg_match_all("/<img(.*)src=\"([^\"]+)\"[^>]+>/isU",$content,$matches);
 
        $img = "";
        if(!empty($matches)) {
        //注意,上面的正则表达式说明src的值是放在数组的第三个中
            $img = $matches[2];
        }else {
            $img = "";
        }
        if (!empty($img)) {
            // $img_url = "http://www.test.com/";

            $patterns= array();
            $replacements = array();

            foreach($img as $imgItem){
                $final_imgUrl = $this->testBaseUpload($imgItem);
                if($final_imgUrl) {
                    $replacements[] = $final_imgUrl;
                    // $img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";
                    $img_new = $imgItem;
                    $patterns[] = $img_new;
                }else {
                    // $replacements[] = $imgItem;
                    // $img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";
                    // $patterns[] = $img_new;
                }
            }

            //让数组按照key来排序
            ksort($patterns);
            ksort($replacements);
            // dd($patterns,$replacements);
            
            //替换内容
            // $vote_content = preg_replace($patterns, $replacements, $content);
            $vote_content = $content;
            foreach($patterns as $k=>$v) {
                $vote_content = str_replace($v,$replacements[$k],$vote_content);
            }
            // dd($vote_content,$content);
            return $vote_content;
        }else {
            // 若没有匹配到图片则返回原来富文本
            return $content;
        }
        
    }
}

2, 如何调用

// 在存储到表数据里面的时候调取方法处理对应的富文本上传内容,然后保存到表中
$articleController = new \App\Http\Controllers\ArticleController();
// $content 富文本内容,图片是 base64 编码保存 
// $data 是处理后的数据,把 img 标签里面的 base64 转成本地图片上传然后返回地址,替换回 src 
$data = $articleController->replaceImgUrl($content); 

3,阿里云存储配置与使用

https://github.com/jacobcyl/Aliyun-oss-storage

相关文章

网友评论

      本文标题:网页富文本编辑器如何将默认 base64 图片上传衔接为服务器保

      本文链接:https://www.haomeiwen.com/subject/hihqtltx.html