Editor.md上传图片处理

首先使用Composer安装ThinkPHP5的图像处理类库:

composer require topthink/think-image

在base类中添加一个接收图片处理方法

public function upload(Request $request){
        // 获取表单上传文件
        //$file = $request->file('file');
        $file = \think\Image::open(request()->file('editormd-image-file'));
        $dirTime = iconv("UTF-8", "GBK", ROOT_PATH . 'public' . DS . 'upload/images/'.date('Ym',time()));
        if (file_exists($dirTime));
        else
            mkdir ($dirTime,0777,true);
        $fileUrl = $dirTime. DS .md5(time()).'.jpg';
        $width = $file->width();
        if ($width >= 1280){
            $info = $file->thumb(1280,720)->save($fileUrl);
        }else{
            $info = $file->save($fileUrl);
        }
        if ($info) {
            $faceurl = strstr($fileUrl,"/upload");
            $result = array(
                'success'=>1,
                'message'=>'上传成功啦!',
                'url'=>$faceurl
            );
            return json_encode($result);
        } else {
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }

在编辑器中加入上传地址的接口

<script type="text/javascript">
        let testEditor;
        $(function() {
            testEditor = editormd("test-editormd", {
                width: "100%",
                height: '60vh',
                path : '/static/markdown/lib/',
                codeFold : true,
                saveHTMLToTextarea : true,
                searchReplace : true,
                htmlDecode : "style,script,iframe|on*",
                imageUpload : true,
                imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL : "/admin/base/upload",
            });
        });
</script>
Comments: 0

「人生在世,留句话给我吧」

提交评论