<script type="text/javascript" src="/statics/js/kindeditor/kindeditor-min.js"></script>
<script type="text/javascript" src="/statics/js/kindeditor/zh_CN.js"></script>
<script type="text/javascript" src="/statics/js/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="/statics/js/kindeditor/default/default.css"></script>
<script charset="utf-8" src="/statics/js/kindeditor/plugins/code/prettify.js"></script>
<link rel="stylesheet" href="/statics/js/kindeditor/plugins/code/prettify.css" />
html:
<div class="form-group">
<label class="col-md-2 control-label">文章内容:</label>
<div class="col-md-9">
(分页符为:
<span style="color: red">#p#分页-标题#e#)</span>
<span>
<label>
<input id="autolitpic" type="checkbox">
提取第一个图片为缩略图
</label>
(
<a class="qximg" href="javascript:void();">[取消缩略图]</a>
)
</span>
<span style="color: red">*</span>
<div class="onShow" id="dr_content_tips">必填项,可无限输入,没有字数限制.</div>
<label class="textarea"><textarea name="content" id="content" class="content" onblur="content_validate()">{$info.content|default=''}</textarea>
<input type="hidden" name="parse" value="0"> <script
type="text/javascript">
var editor_content1;
KindEditor.ready(function(K) {
editor_content1 = K.create('textarea[name="content"]', {
allowFileManager : false,
themesPath: K.basePath,
width: '900px',
height: '700px',
resizeType: 1,
pasteType : 2,
urlType : 'absolute',
cssPath : '/statics/js/kindeditor/plugins/code/prettify.css',
uploadJson : '{:U('ke_upimg')}',
afterCreate : function() {
this.sync();
},
afterBlur:function(){
this.sync();
}
});
prettyPrint();
});
</script>
</label>
</div>
</div>
//后台代码
//keditor编辑器上传图片处理
public function ke_upimg(){
/* 返回标准数据 */
$return = array('error' => 0, 'info' => '上传成功', 'data' => '');
$img = $this->upload();
/* 记录附件信息 */
if($img){
$return['url'] = $img['fullpath'];
unset($return['info'], $return['data']);
} else {
$return['error'] = 1;
$return['message'] = session('upload_error');
}
/* 返回JSON数据 */
exit(json_encode($return));
}
/* 上传图片 */
public function upload(){
session('upload_error', null);
/* 上传配置 */
$setting = C('EDITOR_UPLOAD');
/* 调用文件上传组件上传文件 */
$config = array(
'maxSize' => 3145728,
'savePath' => './news/',
'saveName' => array('uniqid',''),
'exts' => array('jpg', 'gif', 'png', 'jpeg'),
'autoSub' => true,
'subName' => array('date','Ymd'),
);
$this->uploader = new \Think\Upload($config, 'Local');
$info = $this->uploader->upload($_FILES);
//Log::write(print_r($info));
if($info){
$url = '/Uploads'.$info['imgFile']['savepath'].$info['imgFile']['savename'];
$url = str_replace('./', '/', $url);
$info['fullpath'] = __ROOT__.$url;
}
session('upload_error', $this->uploader->getError());
return $info;
}
/**
* 上传图片
*/
public function uploadPicture(){
//TODO: 用户登录检测
/* 返回标准数据 */
$return = array('status' => 1, 'info' => '上传成功', 'data' => '');
$config = array(
'maxSize' => 3145728,
'savePath' => './news/',
'saveName' => array('uniqid',''),
'exts' => array('jpg', 'gif', 'png', 'jpeg'),
'autoSub' => true,
'subName' => array('date','Ymd'),
);
$this->uploader = new \Think\Upload($config, 'Local');
$info = $this->uploader->upload($_FILES);
/* 记录图片信息 */
if($info){
$url = '/Uploads'.$info['download']['savepath'].$info['download']['savename'];
$url = str_replace('./', '/', $url);
$return['path'] = __ROOT__.$url;
$return['id'] = 111;
$return['status'] = 1;
$return = array_merge($info['download'], $return);
} else {
$return['status'] = 0;
$return['info'] = $this->uploader->getError();
}
/* 返回JSON数据 */
$this->ajaxReturn($return);
}
js脚本,提取第一张图片:
//获取第一张图片
$('#autolitpic').click(function (){
if($(this).attr('checked')){
$(this).attr('checked',false);
}else {
$(this).attr('checked',true);
var content=$('.content').val();
if(content.match(/src="[^"]+"/g)==null){
alert('文章内容没有图片');
return;
}else{
var strcount=content.match(/src="[^"]+"/g)[0].replace("src=\"","");
var val=strcount.substring(0,strcount.length-1).replace("http://cb2013.tdedu.org","");
$("#thumb_img").attr('src',val);
$("#img").val(val);
}
}
});
//取消缩略图
$('.qximg').click(function (){
$("#thumb_img").attr('src',"");
$("#img").val("");
});