zoukankan      html  css  js  c++  java
  • thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印。博主是新手,在这里卡住了很久(>_<)

    thinkphp 3.2.3整合ueditor 1.4 下载地址:https://github.com/Nintendov/Ueditor-thinkphp

    下载下来,看着配置就可以了。

    下面就是给上传图片加水印: (在做这步前,请确保ueditor已经正常工作)

    我的工程目录如下:

    fonts里面的fz.fft为水印字体 

    images里面的logo.png为水印图片

    上传的图片储存在 Upload/lmage文件夹里

    下面添加水印的时候需要用到这些文件的路径。

    下面,打开Ueditor.class.php 类  文件在ThinkPHP -> Library -> Org-> Util 里面,就是你导入的Ueditor.class.php类文件

    定位到这个方法:

     主要在红框里面添加几行代码,  它这里是直接返回上传的文件,我们要在这里添加水印再让它返回。

    修改代码如下:

        /**
         * 上传文件方法
         * 
         */
        private function uploadFile($config,$fieldName){
            
            
            $upload = new ThinkUpload();
            $upload->maxSize   =     $config['maxSize'] ;// 设置附件上传大小
            $upload->exts      =     $this->format_exts($config['allowFiles']);// 设置附件上传类型
            $upload->rootPath  =     '.'.$this->rootpath; // 设置附件上传根目录
            $upload->autoSub = false;
            $upload->savePath  =     $this->getFullPath($config['pathFormat']); // 设置附件上传(子)目录
            $info=$upload->uploadOne($_FILES[$fieldName]);
            $rootpath = $this->rootpath;
            
            if(!$info){
                $data = array(
                    "state"=>$upload -> getError(),
                );
            }else{
                
                //-----------------这里是新加的处理水印的代码 begin------------------------//
                $path  = './'.$rootpath.$info['savepath'].$info['savename'];
                $image = new ThinkImage();
                
                //添加文字水印
                $image->open($path)->text('ThinkPHP','./Public/fonts/fz.TTF',20,'#ff0000',ThinkImage::IMAGE_WATER_SOUTHEAST)->save($path); 
                
                //添加图片水印
                //$image->open($path)->water('./Public/images/logo.png',ThinkImage::IMAGE_WATER_NORTHWEST)->save($path);
                
                //--------------------------------end----------------------------//
                
                $data = array(
                    'state'=>"SUCCESS",
                    'url'=>VinFileStorage::getPath($rootpath,$info['savepath'].$info['savename']),
                    'title'=>$info['savename'],
                    'original'=>$info['name'],
                    'type'=>'.' . $info['ext'],
                    'size'=>$info['size'],
                );
            }
            return json_encode($data);
        }
    

    就添加这么4行代码,原来的代码没有修改。

    完成如下:

    遇到的问题:不存在的图像文件

    这是由于文件的地址获取有误造成的,我也在这里弄的很久,上面的$path 获取路径前面要加" ./ " 才能正常访问。(不要问我为什么~)

    ueditor 上传图片按钮点击有几秒的延迟问题,已经有大神解决这个问题了

    http://www.cnblogs.com/liangjiang/p/5799984.html

    完成,博主亲测可用~     (>_<)

  • 相关阅读:
    Angularjs 中文版API v1.3.9 阅读
    jquery之ajax
    requirejs中 shim
    reqiurejs学习
    angularjs与require的集成摘抄
    安装go-admin
    多种GitHub加速方式
    git初始化仓库操作
    git cherry-pick
    gc问题定位
  • 原文地址:https://www.cnblogs.com/zhangans/p/6111823.html
Copyright © 2011-2022 走看看