服务器搭建微信编辑器,ueditor编辑器实现微信上传和图片服务器上传

本文介绍了如何将UEditor编辑器与后端PHP结合,实现图片上传功能。当图片上传时,若设置为图文模式,会调用微信API将图片上传到微信服务器作为永久素材,否则则保存在本地服务器。涉及的技术包括UEditor、微信接口、文件上传处理及错误处理。

如题。

前端部分:

var ue = UE.getEditor('container', {

serverUrl:'{pigcms::$f_siteUrl}<?php  echo U("Home/Upload/UeditorUpload"); ?>'

});

ue.ready(function() {

ue.execCommand('serverparam', {

'token': '{pigcms::$token}',

'isTuwen':1,

});

});

后端部分:

class UploadAction extends BaseAction {

public $token;

public function _initialize() {

parent::_initialize();

$this->token=$this->_session('token');

if (!$this->token){

$this->token='admin';

}

}

/**

* 自定义ueditor文件上传

*/

public function UeditorUpload(){

switch($_GET['action']){

case 'config':

$config = C('ueditor_config');

echo json_encode($config);

exit;

break;

case 'uploadimage':

if($_GET['isTuwen'] == 1){

$token = $_GET['token'];

$wxuser = M('Wxuser')->where(array('token'=>$token))->find();

if(!empty($wxuser)){

import('ORG.Net.UploadFile');

$upload = new UploadFile();// 实例化上传类

$upload->maxSize  = intval(C('up_size'))*1024;// 设置附件上传大小

$upload->allowExts  = explode(',',C('up_exts'));// 设置附件上传类型

$upload->savePath =  C('up_path');// 设置附件上传目录

if(!$upload->upload()) {// 上传错误提示错误信息

$re = array();

$re['state'] = $upload->getErrorMsg();

echo json_encode($re);

exit;

}else{// 上传成功 获取上传文件信息

$info =  $upload->getUploadFileInfo();

}

$pic_path = $info['0']['savepath'].$info['0']['savename'];

$data['media']='@'.realpath($pic_path);

$weouth_new = new Weouth_new($wxuser['appid']);

$result = $weouth_new->uploadForeverMedia($data, 'image');

unlink(realpath($pic_path));//删除已上传的图片

if($result){

$re = array();

$re['state'] = "SUCCESS";

$re['url'] = $result['url'];

$re['title'] = $_FILES['upfile']['name'];

$re['original'] = $result['url'];

}else{

$re = array();

$re['state'] = $weouth_new->errMsg;

}

}

echo json_encode($re);

exit;

}else{

$return=$this->localUpload();

if($return['error']){

$re = array();

$re['state'] = $return['msg'];

}else{

$re = array();

$re['state'] = "SUCCESS";

$re['url'] = $return['msg'];

$re['title'] = $_FILES['upfile']['name'];

$re['original'] = $return['msg'];

}

echo json_encode($re);

exit;

}

break;

}

}

public function upload(){

if (!function_exists('imagecreate')){

exit('php不支持gd库,请配置后再使用');

}

if (IS_POST){

$return=$this->localUpload();

echo "";

echo("");

exit();

}

}

/**

* 线上上传方法

*/

public function uploadImg(){

$return=$this->localUpload();

if ($return['error']){

$this->alert($return['msg']);

}else {

header('Content-type: text/html; charset=UTF-8');

echo json_encode(array('status' => 1, 'path' => $return['msg'],'small_path'=>$return['small'],'filename'=>$return['filename']));

exit;

}

}

/**

* 线上上传音乐

*/

public function uploadMusic(){

$return=$this->localUpload('mp3');

if ($return['error']){

$this->alert($return['msg']);

}else {

header('Content-type: text/html; charset=UTF-8');

echo json_encode(array('status' => 1, 'path' => $return['msg'],'small_path'=>$return['small'],'filename'=>$return['filename']));

exit;

}

}

public function localUpload($filetypes=''){

$file_input_name="";

$file_info=array();

foreach ($_FILES as $key=>$file){

if(!empty($file['name'])) {

$file_input_name=$key;

$file_info['size']=$file['size'];

$pathinfo = pathinfo($file['name']);

$file_info['extension']=$pathinfo['extension'];

break;

}

}

if($file_input_name){

$uf = new UploadFile_new($file_input_name);

$uf->setMaxSize(intval(C('up_size'))) ;//KB

if (!$filetypes){

$upload_allowExts  = implode("|",explode(',',C('up_exts')));

}else {

$upload_allowExts  = $filetypes;

}

$uf->setFileType($upload_allowExts);

if (isset($_POST['width'])&&intval($_POST['width'])){

$isneed_Resize=true;

$uf->setResizeImage(true);

$uf->setResizeWidth($_POST['width']);

$uf->setResizeHeight($_POST['height']);

}

$uf->setUploadType("ftp");//设置上传方式,默认为程序上传

$uf->setShowAsChinese(true);

if(($rtnMSG=$uf->upload())=="success")

{

$error=0;

if($isneed_Resize){

$msg = $uf->getResizeImageURL();

}else{

$msg = $uf->getSaveFileURL();

}

$filename=$uf->getFileName();

$small_img_URL= $uf->getResizeImageURLForMobile();

}else{

$error=1;

$msg=$rtnMSG;

$small_img_URL='';

}

}else{

$error=1;

$small_img_URL='';

$msg="请选择图片";

}

return array('error'=>$error,'msg'=>$msg,'small'=>$small_img_URL,'filename'=>$filename);

}

public function alert($msg) {

header('Content-type: text/html; charset=UTF-8');

echo json_encode(array('error' => 1, 'message' => $msg));

exit;

}

/**

* 线下上传图片

*/

public function uploadImg_test(){

$path = '';

$result = array();

import('@.ORG.Util.Upload');

$uf = new Upload('Filedata');//file

$uf->setMaxSize(102400);

$uf->setUploadType("ftp");

$uf->setServerSite('20');

$uf->setSaveDir("/klf/users/album/");

$uf->setResizeReality(true);

$rtnMSG=$uf->doupload();

if($rtnMSG=="success"){

$path=$uf->getSaveFileURL();

$filename=$uf->getFileName();

//wss 获取上传名称

$result = array('status'=>"1",'path'=>$path,'filename'=>$filename);

echo json_encode($result);

die();

}else{

$result = array('status'=>"0",'path'=>$path);

echo json_encode($result);

die();

}

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值