From 16e970fa2ee05dfc07bae10d58add4f3c6aa3243 Mon Sep 17 00:00:00 2001 From: xiaoz Date: Thu, 11 Apr 2019 14:22:18 +0800 Subject: [PATCH] api support --- application/config/routes.php | 2 ++ application/controllers/Upload.php | 38 ++++++++++++++++++++++++++---- application/libraries/Image.php | 2 ++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/application/config/routes.php b/application/config/routes.php index 6bef8da..2e73996 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -52,4 +52,6 @@ defined('BASEPATH') OR exit('No direct script access allowed'); $route['default_controller'] = 'home'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; +//API路由 $route['api/upload'] = 'upload/localhost'; +$route['api/upload/(:any)'] = 'upload/localhost/$1'; \ No newline at end of file diff --git a/application/controllers/Upload.php b/application/controllers/Upload.php index 3cd853b..1014ade 100644 --- a/application/controllers/Upload.php +++ b/application/controllers/Upload.php @@ -4,6 +4,9 @@ author:xiaoz.me QQ:337003006 */ + + //允许跨域请求 + header("Access-Control-Allow-Origin: *"); defined('BASEPATH') OR exit('No direct script access allowed'); class Upload extends CI_Controller{ @@ -68,7 +71,7 @@ $config['encrypt_name'] = TRUE; //随机命名图片 return $config; } - public function localhost(){ + public function localhost($type = 'json'){ //加载上传的配置选项 $config = $this->config(); //加载上传类 @@ -140,7 +143,9 @@ "width" => $data['image_width'], "height" => $data['image_height'] ); - $this->succeed_msg($info); + //$this->succeed_msg($info); + //根据不同的类型返回不同的数据 + $this->re_data($type,$info); } //图片没有上传过 else{ @@ -180,10 +185,33 @@ "width" => $data['image_width'], "height" => $data['image_height'] ); + //根据不同的类型返回不同的数据 + $this->re_data($type,$info); } - //var_dump($info); - //exit; - $this->succeed_msg($info); + } + } + //根据不同的类型返回不同的数据 + protected function re_data($type,$info){ + $url = $info['url']; + switch ($type) { + case 'json': + $this->succeed_msg($info); + break; + case 'url': + echo $url; + break; + case 'html': + echo ""; + break; + case 'markdown': + echo "![]($url)"; + break; + case 'bbcode': + echo "[img]".$url."[/img]"; + break; + default: + $this->succeed_msg($info); + break; } } //上传成功返回json diff --git a/application/libraries/Image.php b/application/libraries/Image.php index 27d67e2..e32546a 100644 --- a/application/libraries/Image.php +++ b/application/libraries/Image.php @@ -42,6 +42,7 @@ $image->writeImage( $thumbnail_full ); //清理工作 $image->clear(); + return TRUE; } //不支持ImageMagick,使用GD2进行裁剪 else{ @@ -54,6 +55,7 @@ $config['height'] = $height; $this->CI->load->library('image_lib', $config); $this->CI->image_lib->resize(); + return TRUE; } } //图片像素太小了,不创建缩略图