ThinkCMF图片在线管理_编辑器

很多时候,我们在添加发布文章的时候会重复使用同一张图片,或者添加一张已上传的图片。

ThinkCMF虽然自带有网络地址方式,但资源地址查询起来又非常麻烦。

重复传入图片的话,又有点浪费服务器资源。

所以我们就来添加一个图片在线管理。

ThinkCMF版本:5.1

效果图

2efc1b1c29722c496c142679f9ac2bfa.png

修改HTML结构

文件地址:.\ThinkCMF\public\static\js\ueditor\dialogs\image\image.html

在image.html找下面这段代码,取消注释

<span class="tab" data-content-id="online"><var id="lang_tab_online"></var></span>

增加PHP函数:

文件地址:.\ThinkCMF\app\user\controller\UeditorController.php

5.1版本文件地址:.\ThinkCMF\\vendor\thinkcmf\cmf-app\src\user\controller\UeditorController.php

    /**
     * 自定义函数-图片在线管理
     */
    private function _get_listimage(){
        $allowFiles = array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ); //文件允许格式
        $listSize = 3000; //文件大小限制,单位KB
		//图片路径
		$path = 'upload';	 //htdocs/app/user/controller 线上地址
        $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);

        /* 获取参数 */
        $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
        $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
        $end = $start + $size;

        /* 获取文件列表 */
        $path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "":"/") . $path;
        
        $files = $this->getfiles($path, $allowFiles);
        if (!count($files)) {
            return json_encode(array(
                "state" => "no match file",
                "list" => array(),
                "start" => $start,
                "total" => count($files)
            ));
        }

        /* 获取指定范围的列表 */
        $len = count($files);
        for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
		
            $list[] = $files[$i];
        }
		
        /* 返回数据 */
        $result = array(
            "state" => "SUCCESS",
            "list" => $list,
            "start" => $start,
            "total" => count($files)
        );

        return json_encode($result);
    }

    /**
     * 自定义函数-遍历获取目录下的指定类型的文件
     */
    private function getfiles($path, $allowFiles, &$files = [])
    {
        if (!is_dir($path)) return null;
        if (substr($path, strlen($path) - 1) != '/') $path .= '/';
        $handle = opendir($path);
        while (false !== ($file = readdir($handle))) {
            if ($file != '.' && $file != '..') {
                $path2 = $path . $file;
                if (is_dir($path2)) {
                    $this->getfiles($path2, $allowFiles, $files);
                } else {
                    if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
                        $files[] = [
                            'url'   => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
                            'mtime' => filemtime($path2)
                        ];
                    }
                }
            }
        }
        return $files;
    }

图片上传处理

文件地址:.\ThinkCMF\app\user\controller\UeditorController.php

5.1版本文件地址:.\ThinkCMF\\vendor\thinkcmf\cmf-app\src\user\controller\UeditorController.php

查找代码段:

 /* 列出图片 */
case 'listimage':
        $result = "";
        break;

修改为:

 /* 列出图片 */
case 'listimage':
        $result = $this->_get_listimage();
        break;

ThinkCMF后台上传图片有两个地方,另一个缩略图哪里明天再写吧!

Comments 0

0.154191s