服务电话:136 2446 7185 于先生
常见问题 付款方式 加入收藏
首页 公司简介 业务介绍 风格模板 资讯中心 案例展示 项目拓展 联系我们
长春地区网站建设,做各类网站 咨询电话:136 2446 7185
资讯中心 更多>>
微信朋友圈与公众号营销的100
那么用户体验的开始可以从哪些方
围绕关键词优化规划整理出需要做
如何申请谷歌google网站广
红松林 生态视频阅读第一品牌
2018年春节期间移动数据流量
从四大方面解说微信朋友圈营销并
服务方案 更多>>
年年丰收,吉林省年年丰收农业发
吉林经济网是吉林省地区权威专业
吉林省书画院是吉林省专业书画创
吉林省长春微信三级分销系统功能
美国卡夫盾石油化工有限公司
合肥安嘉生物科技有限公司
福尔安全顾问公司 吉林省调查公
联系方式 更多>>
地址:长春市亚泰大街与自由大路交汇五环国际大厦1408、1409室
联系人:于先生
全国咨询热线:400-915-4435
邮箱:xgnic@xgnic.com
手机:13624467185 13844844006
网址:http://www.xgsite.com
 
常见问题 >> ueditor远程图片本地化的实现

ueditor远程图片本地化的实现

点击率:3118   发布人:管理员    发布日期:2018/3/25   【去百度看看】

前不久客户反馈说,在秀米网上编辑好文章,发布到我们后台,前台图片无法显示,最先想到的是图片域名限制。发邮件给秀米,秀米给出的建议是类似微信后台做图片本地化。

开发组内部讨论后,看到了这篇文章,UEditor编辑器如何关闭抓取远程图片本地化功能,so easy?按文章说明设置,测试,失败!

百度看了下,大都是这样解答,说明应该是有人实现了,秀米编辑器核心代码也正是ueditor核心代码,那就只能自己捣鼓了。

具体捣鼓过程如下:

注意:捣鼓前先备份下,以备修改错误导致其他问题

  1. 打开ueditor.config.js,在配置项中加入

,catchRemoteImageEnable:true
  1. 打开ueditor.config.js,搜索catchremoteimage,在加入console.warn(url),看看上传地址。前台测试后看到/Skin/public/ueditor/php/controller.php?action=catchimage

  2. 打开php/controller.php 可以看到

case 'catchimage':
        $result = include("action_crawler.php");        
        break;

接着打开统计目录下action_crawler.php

include("Uploader.class.php");
$item = new Uploader($imgUrl, $config, "remote");

看到这两段代码。继续打开统计目录下Uploader.class.php

//构造函数如下
public function __construct($fileField, $config, $type = "upload")
    {        
    $this->fileField = $fileField;        
    $this->config = $config;        
    $this->type = $type;
        if ($type == "remote") {            
        $this->saveRemote();
        } else if($type == "base64") {            
        $this->upBase64();
        } else {            
        $this->upFile();
        }        
        $this->stateMap['ERROR_TYPE_NOT_ALLOWED'] = iconv('unicode', 'utf-8', $this->stateMap['ERROR_TYPE_NOT_ALLOWED']);
    }

搜索saveRemote,主要修复fileType与oriName两块。

/**
     * 拉取远程图片
     * @return mixed
     */
    private function saveRemote()
    {        
        $imgUrl = htmlspecialchars($this->fileField);
       $imgUrl = str_replace("&", "&", $imgUrl);

        //http开头验证
        if (strpos($imgUrl, "http") !== 0) { 
            $this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");
            return;
        }

        preg_match('/(^https*:\/\/[^:\/]+)/', $imgUrl, $matches);
        $host_with_protocol = count($matches) > 1 ? $matches[1] : '';

        // 判断是否是合法 url
        if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) {            
            $this->stateInfo = $this->getStateInfo("INVALID_URL");
            return;
        }

        preg_match('/^https*:\/\/(.+)/', $host_with_protocol, $matches);        
        $host_without_protocol = count($matches) > 1 ? $matches[1] : '';

        // 此时提取出来的可能是 ip 也有可能是域名,先获取 ip        
        $ip = gethostbyname($host_without_protocol);
        // 判断是否是私有 ip
        if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {            
            $this->stateInfo = $this->getStateInfo("INVALID_IP");
            return;
        }

        //获取请求头并检测死链        
        $heads = get_headers($imgUrl, 1);
        if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
            $this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");
            return;
        }
        //格式验证(扩展名验证和Content-Type验证)        
        $fileType = strtolower(strrchr($imgUrl, '.'));
        //13sai 20170712  秀米网链接接如下http://img.xiumi.us/xmi/ua/h4qG/i/b8f2af6986e8dba51615a9d85cc82f3b-sz_1952250.JPG?x-oss-process=style/xm ,我们完善下        
        $fileType = (strpos($fileType, '?') > 0)? strtolower(substr($fileType,0,strpos($fileType,'?'))) : strtolower($fileType);
        //echo $fileType;die();
        if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {            $this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");
            return;
        }

        //打开输出缓冲区并获取远程图片
        ob_start();        $context = stream_context_create(
            array('http' => array(                'follow_location' => false // don't follow redirects
            ))
        );
        readfile($imgUrl, false, $context);
        $img = ob_get_contents();
        ob_end_clean();
        //13sai 20170712  此处正则有问题,修改如下
        //preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
        preg_match("/\/[A-za-z0-9-]+.".$fileType."/", strtolower($imgUrl), $m);
        //var_dump($m);die();

        $this->oriName = $m ? ltrim($m[0],'/'):"";
        //$this->oriName = $m ? $m[1]:"";
        //echo $this->oriName;
        //die();
        $this->fileSize = strlen($img);
        $this->fileType = $this->getFileExt();
        $this->fullName = $this->getFullName();
        $this->filePath = $this->getFilePath();
        $this->fileName = $this->getFileName();
        $dirname = dirname($this->filePath);

        //检查文件大小是否超出限制
        if (!$this->checkSize()) {
            $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
            return;
        }

        //创建目录失败
        if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
            $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
            return;
        } else if (!is_writeable($dirname)) {
            $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
            return;
        }

        //移动文件
        if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
            $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
        } else { //移动成功
            $this->stateInfo = $this->stateMap[0];
        }

    }
  1. 打开php/config.json,修改大小、格式、存储路径等参数

/* 抓取远程图片配置 */    "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],    "catcherActionName": "catchimage", /* 执行抓取远程图片的action名称 */    "catcherFieldName": "source", /* 提交的图片列表表单名称 */    "catcherPathFormat": "/Upload/ueditor/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */    "catcherUrlPrefix": "", /* 图片访问路径前缀 */    "catcherMaxSize": 20480000, /* 上传大小限制,单位B */    "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取图片格式显示 */

5.保存修改上传,测试成功!


打印本页  关闭本页
构站网 免费网站建设 长春网站建设团队 长春网站制作业务介绍 长春网站设计 长春网站建设公司 长春网站建设流程

版权所有 吉林省新格信息技术有限公司 【安徽省新格通达电子商务有限公司】 地址1:长春市南关区体育场五环国际大厦1408室  地址2:合肥市滨湖新区云谷路与江西路交口未来领地A座1011室

皖ICP备2021004516号-1 邮箱:web@4435.cn 手机: 136 2446 7185  13844844006


吉公网安备 22010202000117号