****CI和UEditor集成
百度UEditor是一款比较常用编辑器
下载地址:
http://ueditor.baidu.com/website/download.html
1.在assets目录下建立ueditor文件夹,把下载的源码放入该文件夹
2.在需要使用ueditor的文件内引入ueditor相关文件
上代码:
<html>
<head>
<title>完整demo</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" charset="utf-8" src="<?php echo base_url().'assets/ueditor/;?>ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="<?php echo base_url().'assets/ueditor/;?>ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="<?php echo base_url().'assets/ueditor/;?>lang/zh-cn/zh-cn.js"></script>
<style type="text/css">
div{
width:100%;
}
</style>
</head>
<body>
<div>
<h1>完整demo</h1>
<script name="content" id="editor" type="text/plain" style="width:1024px;height:500px;">
初始化内容
</script>
</div>
<script type="text/javascript">
var ue = UE.getEditor('editor');
</script>
</body>
</html>
CI框架整合UEditor编辑器上传功能
最近项目中要使用到富文本编辑器,选用了功能强大的UEditor,接下来就来讲讲UEditor编辑器的上传功能整合。
本文UEditor版本:ueditor1_4_3_utf8_php版本
第一步:部署编辑器
HTML代码:
1 <textarea id="editor" class="editor" type="text/plain" style="width:100%;height:500px;"></textarea>
JavaScript代码:
1 $(document).ready(function () {
2 var ue = UE.getEditor('editor',{
3 serverUrl:'/ueditorup/unifiedRequest/',//后台统一请求路径
4 autoHeightEnabled:false,
5 toolbars:
6 [[
7 'fullscreen', 'source', '|', 'undo', 'redo', '|',
8 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
9 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
10 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
11 'directionalityltr', 'directionalityrtl', 'indent', '|',
12 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
13 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
14 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
15 'horizontal', 'date', 'time', 'spechars', '|',
16 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
17 'print', 'preview', 'searchreplace', 'help', 'drafts'
18 ]],
19 });
第二步:服务端整合
前端代码部署完,现在页面已经可以正常显示百度编辑器的编辑框了,接下来就是本文要介绍的上传功能的整合。
首先在CI框架的controllers目录下创建名为ueditorup的.php文件并在此文件创建同名的类(UeditorUp),百度编辑器的所有上传功能都将在这个类里实现(图片、涂鸦、视频,附件上传)。
下面代码中的上传处理类MyUploader 就是UEditor中的Uploader.class.php文件,这里为了与前端编辑器上传功能完美衔接使用了UEditor自带的Uploader.class.php文件而没有使用CI框架的上传处理功能(本人对UEditor不是很熟悉),不过为了让上传更加安全,增加了上传文件的MIME类型判断,判断代码就直接来自CI框架的上传类,配置都放在mimeconfig.php配置文件中。
而配置文件uploadconfig则是UEditor编辑器的config.json文件配置,只是把json格式改成了CI的数组格式。
UEditor编辑器个服务器交互都是通过统一请求地址进行访问的,同时会通过GET方法提交action的值,服务器端则通过action的值判断具体的处理方法。
<?php
//ueditorup.php
class UeditorUp extends MY_Controller
{
function __construct()
{
parent::__construct();
} /**
* 百度编辑器唯一请求接口
* @throws Exception
*/
public function unifiedRequest ()
{
try
{
$action = $this->input->get('action');
$this->config->load('uploadconfig');//获取上传配置
$config = $this->config->item('ueditor_upload');
if(empty($config))
throw new Exception(errorLang('62409'));if($action == 'config')
{
echo json_encode($config);
}elseif(method_exists($this, $action))
{
$this->config->load('mimeconfig');
$config['mimeType'] = $this->config->item('mime_type_conf');
$result = $this->{$action}($config);
echo json_encode($result);
}else
throw new Exception(errorLang('62409'));
}
catch (Exception $e)
{
echo json_encode(array('state'=>$e->getMessage()));
}
} /**
* 图片上传处理方法
* @param array $config
*/
public function imageUpload ($config)
{
$this->load->library('MyUploader');
$config = $this->setConf($config, 'image');
$this->myuploader->do_load($config['imageFieldName'], $config);
return $this->myuploader->getFileInfo();
}
/**
* 视频上传处理方法
* @param array $config
*/
public function videoUpload ($config)
{
$this->load->library('MyUploader');
$config = $this->setConf($config, 'video');
$this->myuploader->do_load($config['videoFieldName'], $config);
return $this->myuploader->getFileInfo();
}
/**
* 附件上传处理方法
* @param array $config
*/
public function filesUpload ($config)
{
$this->load->library('MyUploader');
$config = $this->setConf($config, 'file');
$this->myuploader->do_load($config['fileFieldName'], $config);
return $this->myuploader->getFileInfo();
}
/**
* 涂鸦图片上传处理方法
* @param unknown $config
*/
public function scrawlUpload ($config)
{
$this->load->library('MyUploader');
$config = $this->setConf($config, 'scrawl', 'scrawl.png');
$this->myuploader->do_load($config['scrawlFieldName'], $config, 'base64');
return $this->myuploader->getFileInfo();
} /**
* 设置config
* @param array $config
* @param string $prefix
* @param string $oriName
* @return array
*/
private function setConf (array $config, $prefix, $oriName=NULL)
{
$config['maxSize'] = array_key_exists($prefix.'MaxSize', $config) ? $config[$prefix.'MaxSize'] : $config['fileMaxSize'];
$config['allowFiles'] = array_key_exists($prefix.'AllowFiles', $config) ? $config[$prefix.'AllowFiles'] : $config['fileAllowFiles'];
$config['pathFormat'] = array_key_exists($prefix.'PathFormat', $config) ? $config[$prefix.'PathFormat'] : $config['filePathFormat'];
empty($oriName) || $config['oriName'] = $oriName;
return $config;
} }
下面是修改后的MyUploader上传类的文件后缀获取方法。
/**
* MyUploader.php
* 获取文件扩展名(MIME)
* @return string
*/
private function getFileExt()
{
$regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
if (function_exists('finfo_file'))
{
$finfo = finfo_open(FILEINFO_MIME);
if (is_resource($finfo))
{
$mime = @finfo_file($finfo, $this->file['tmp_name']);
finfo_close($finfo);
if (is_string($mime) && preg_match($regexp, $mime, $matches))
{
if(array_key_exists($matches[1], $this->config['mimeType']))
{
$type = $this->config['mimeType'][$matches[1]];
return $type;
}
}
}
}
return FALSE;
}
到此CI框架整合UEditor编辑器就算完成了。
*注意:在整合上传功能的时候,要开启文件保存目录的读写权限。
转:
http://www.cnblogs.com/wenxiong/p/3930013.html
参考:
CI框架+Umeditor上传图片配置信息
(注:UMeditor是Ueditor的迷你版)
Umeditor提供了一个上传文件通用的类Uploader.class.php, 首先将Uploader.class.php类放入CI框架的libraries目录下更名为Myuploader.php然后将该类提供的构造方法替换掉
本来的构造方法:
- /**
- * 构造函数
- * @param string $fileField 表单名称
- * @param array $config 配置项
- * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
- */
- 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']);
- }
替换成:
- /**
- * 构造函数
- * @param string $fileField 表单名称
- * @param array $config 配置项
- * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
- */
- public function __construct()
- {
- }
- public function Init($fileField , $config , $base64 = false)
- {
- /*var_dump($fileField);
- var_dump($config);exit;*/
- $this->fileField = $fileField;
- $this->config = $config;
- $this->stateInfo = $this->stateMap[ 0 ];
- $this->upFile( $base64 );
- }
然后创建上传文件的方法:
- /*Ueditor_model*/
- class Ueditor_model extends CI_Model {
- function __construct() {
- parent::__construct();
- $this->load->library("myuploader");
- }
- function upload_image(){
- $dir = 'source/uploads/images/ueditor_images/';
- if (!is_dir($dir)) {
- $res = mkdir($dir, 0755, true);
- }
- //上传配置
- $config = array(
- "savePath" => $dir , //存储文件夹
- "maxSize" => 512, //允许的文件最大尺寸,单位KB
- "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" ) //允许的文件格式
- );
- $config[ "savePath" ] = $dir;
- $this->myuploader->init("upfile", $config, $base=false);
- $info = $this->myuploader->getFileInfo();
- return $info;
- }
- }
- /*controller*/
- class Uploads_files extends CI_Controller {
- function goods_edition_upload_img() {
- $info = $this -> ueditor_model -> upload_image();
- echo json_encode($info);
- }
- }
最后一步到umeditor.config.js中修改上传文件方法
- /**
- * 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
- */
- window.UMEDITOR_CONFIG = {
- //为编辑器实例添加一个路径,这个不能被注释
- UMEDITOR_HOME_URL : URL
- //图片上传配置区
- ,imageUrl:URL + "" <span style="white-space:pre"> </span>//图片上传提交地址
- ,imagePath:URL + "" <span style="white-space:pre"> </span>//图片修正地址,引用了fixedImagePath,如有特殊需求,可自行配置
- ,imageFieldName:"upfile" <span style="white-space:pre"> </span>//图片数据的key,若此处修改,需要在后台对应文件修改对应参数
转:http://blog.csdn.net/demon3182/article/details/41915283
最新文章
- shell 计算
- hdu FatMouse&#39;s Speed 动态规划DP
- [转载]CRect::DeflateRect
- C语言基础课程 第二课 HelloWorld不为菜鸟所知的秘密
- hadoop 2.0 详细配置教程(转载)
- 找出并解决 JavaScript 和 Dojo 引起的浏览器内存泄露问题
- Tcp Socket非阻塞recv
- oracle按某个字段分组然后从每组取出最大的一条纪录
- Android 模块化探索与实践
- RSA简介(三)——寻找质数
- 669. Trim a Binary Search Tree
- toString()方法细节
- JAVA 四舍五入Math.round方法
- python 操作Excel文件
- Tomcat修改service.xml性能调优 增加最大并发连接数
- WPF 自定义控件缩放
- solr创建业务域以及指定中文分析器IK
- 树莓派进阶之路 (035) - 基于linux的zsh安装脚本
- vue inheritAttrs、$attrs和$listeners使用
- 每日英语:Is It Possible To Reason About Having A Child?