Browse Source

api support

dev
xiaoz 6 years ago
parent
commit
16e970fa2e
  1. 2
      application/config/routes.php
  2. 36
      application/controllers/Upload.php
  3. 2
      application/libraries/Image.php

2
application/config/routes.php

@ -52,4 +52,6 @@ defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'home'; $route['default_controller'] = 'home';
$route['404_override'] = ''; $route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE; $route['translate_uri_dashes'] = FALSE;
//API路由
$route['api/upload'] = 'upload/localhost'; $route['api/upload'] = 'upload/localhost';
$route['api/upload/(:any)'] = 'upload/localhost/$1';

36
application/controllers/Upload.php

@ -4,6 +4,9 @@
author:xiaoz.me author:xiaoz.me
QQ:337003006 QQ:337003006
*/ */
//允许跨域请求
header("Access-Control-Allow-Origin: *");
defined('BASEPATH') OR exit('No direct script access allowed'); defined('BASEPATH') OR exit('No direct script access allowed');
class Upload extends CI_Controller{ class Upload extends CI_Controller{
@ -68,7 +71,7 @@
$config['encrypt_name'] = TRUE; //随机命名图片 $config['encrypt_name'] = TRUE; //随机命名图片
return $config; return $config;
} }
public function localhost(){ public function localhost($type = 'json'){
//加载上传的配置选项 //加载上传的配置选项
$config = $this->config(); $config = $this->config();
//加载上传类 //加载上传类
@ -140,7 +143,9 @@
"width" => $data['image_width'], "width" => $data['image_width'],
"height" => $data['image_height'] "height" => $data['image_height']
); );
$this->succeed_msg($info); //$this->succeed_msg($info);
//根据不同的类型返回不同的数据
$this->re_data($type,$info);
} }
//图片没有上传过 //图片没有上传过
else{ else{
@ -180,10 +185,33 @@
"width" => $data['image_width'], "width" => $data['image_width'],
"height" => $data['image_height'] "height" => $data['image_height']
); );
//根据不同的类型返回不同的数据
$this->re_data($type,$info);
}
} }
//var_dump($info); }
//exit; //根据不同的类型返回不同的数据
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 "<img src = '$url' />";
break;
case 'markdown':
echo "![]($url)";
break;
case 'bbcode':
echo "[img]".$url."[/img]";
break;
default:
$this->succeed_msg($info); $this->succeed_msg($info);
break;
} }
} }
//上传成功返回json //上传成功返回json

2
application/libraries/Image.php

@ -42,6 +42,7 @@
$image->writeImage( $thumbnail_full ); $image->writeImage( $thumbnail_full );
//清理工作 //清理工作
$image->clear(); $image->clear();
return TRUE;
} }
//不支持ImageMagick,使用GD2进行裁剪 //不支持ImageMagick,使用GD2进行裁剪
else{ else{
@ -54,6 +55,7 @@
$config['height'] = $height; $config['height'] = $height;
$this->CI->load->library('image_lib', $config); $this->CI->load->library('image_lib', $config);
$this->CI->image_lib->resize(); $this->CI->image_lib->resize();
return TRUE;
} }
} }
//图片像素太小了,不创建缩略图 //图片像素太小了,不创建缩略图

Loading…
Cancel
Save