今天分享一下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 \Think\Upload(); $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 \Think\Image(); //添加文字水印 $image->open($path)->text('ThinkPHP','./Public/fonts/fz.TTF',20,'#ff0000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($path); //添加圖片水印 //$image->open($path)->water('./Public/images/logo.png',\Think\Image::IMAGE_WATER_NORTHWEST)->save($path); //--------------------------------end----------------------------// $data = array( 'state'=>"SUCCESS", 'url'=>\Vin\FileStorage::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
完成,博主親測可用~ (>_<)