Thinkphp uses CKEditor rich text editor

Download address of Ckeditor (Standard Package version is recommended):

http://ckeditor.com/download

1. Introduce the ckeditor core file ckeditor.js in the page 

<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

2. Insert HTML controls where you use the editor

<textarea  id="content" name="content" cols="30" rows="2"></textarea>

Three, replace the corresponding control with the editor code

<script type="text/javascript"
window.onload = function()
{
    CKEDITOR.replace( 'content');
};
</script>

Fourth, configure the editor's picture upload function

1. Remove the text in the upload preview box

Add in ckeditor/config.js:

CKEDITOR.editorConfig = function( config ) {
//去掉图片上传预览区域显示的文字
config.image_previewText=' '; 
});

2. Turn on the upload function (the upload function is hidden, so it needs to be turned on)

In the ckeditor/plugins/image/dialogs/image.js file, search for: id:"Upload",hidden:!0 , change !0 to false

3. In ckeditor/config.js, add the path of the back-end uploading picture:

//图片上传配置
config.filebrowserUploadUrl = '/admin/article/uploadPic';

Five, thinkphp back-end file upload method

    /**
    * @name='上传图片'    
    */
    public function uploadPic()
    {
		$file = request()->file('upload');
        if($file){
			$uploadtype="jpg,png,gif,jpeg"; 
			//限制只能上传2MB图片
			$path=ROOT_PATH . 'public' . DS . 'uploads';
			$info = $file->rule(function(){return md5(microtime(true));})->validate(['size'=>1024*1024*2,'ext'=>$uploadtype])->move($path);							
            if($info){ 
                $img =request()->domain().'/uploads/'.$info->getSaveName(); 
				$callback = $_REQUEST["CKEditorFuncNum"];
				echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($callback,'".$img."','');</script>";
            }else{                
   
				echo '上传失败,请检查文件类型是否允许上传';
            }
        }else{
			echo '请选择要上传的图片';
		}

    }

 

Guess you like

Origin blog.csdn.net/qq15577969/article/details/115264791