<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
private $serverHost;
public function __construct(){
parent::__construct();
$this->serverHost = 'http://file.study.com/';
}
public function fileUpload(){
$path = I("post.path","",'trim');
if(empty($path)){
$array = array("error"=>1,"message"=>"路径不能为空!");
self::message($array);
}
$basePath = preg_replace('/((http|https):\/\/)?\w+\.\w+\.\w+\//', '', $path);
$basePath = rtrim($basePath,"/");
$basePath = $basePath."/img/".date('Ymd',time());
if(empty($basePath)){
$array = array("error"=>1,"message"=>"路径解析错误");
self::message($array);
}
$upload = new \Think\Upload();
$upload->maxSize = 2097152 ;
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');
$upload->rootPath = "./static/";
$upload->autoSub = true;
$upload->subName = $basePath;
$info = $upload->uploadOne($_FILES['imgFile']);
if(!$info){
$array = array("error"=>1,"message"=>$upload->getError());
self::message($array);
}else{
$info['newpath'] = $upload->rootPath.$info['savepath'].$info['savename'];
$status = self::imgResetSize($info);
if($status){
$array = array('error' =>0,'url'=>$this->serverHost.'/'.$basePath.'/'.$info['savename']);
self::message($array);
}else{
$array = array('error' =>1,'message'=> '图片压缩出错!');
self::message($array);
}
}
}
public function formUpload(){
$post = I("post.","","trim");
if(empty($post)){
$array = array('error' =>1,'message'=> '请传入参数!');
self::message($array);
}
if(empty($post['img'])){
$array = array('error' =>1,'message'=> '图片内容不能为空!');
self::message($array);
}
if(strlen($post['img'])>3103540){
$array = array('error' =>1,'message'=> '文件太大!');
self::message($array);
}
if(empty($post['type'])){
$array = array('error' =>1,'message'=> '图片类型不能为空!');
self::message($array);
}
if(!in_array($post['type'], array('jpg', 'gif', 'png', 'jpeg'))){
$array = array('error' =>1,'message'=> '不支持的文件类型!');
self::message($array);
}
if(empty($post['webdir'])){
$array = array('error' =>1,'message'=> '目录不能为空!');
self::message($array);
}
$basePath = preg_replace('/((http|https):\/\/)?\w+\.\w+\.\w+\//', '', $post['webdir']);
$basePath = rtrim($basePath,"/");
$basePath = $basePath."/img/".date('Ymd',time());
$savepath = "static/".$basePath;
if(empty($basePath)){
$array = array("error"=>1,"message"=>"路径解析错误");
self::message($array);
}
if (!is_dir($savepath)){
$res = mkdir($savepath, 0755, true);
if(!$res){
$array = array("error"=>1,"message"=>"目录创建失败!");
self::message($array);
}
}
$file_content = base64_decode($post['img']);
$imgFileName = self::getFileName($savepath,$post['type']);
if(!file_put_contents($savepath.'/'.$imgFileName ,$file_content)){
$array = array("error"=>1,"message"=>"图片保存失败");
self::message($array);
}
$imgResetSize_info = array("newpath"=>$savepath.'/'.$imgFileName, 'ext'=> $post['type']);
$status = self::imgResetSize($imgResetSize_info);
if($status){
$array = array("error"=>0,"url"=>$this->serverHost.'/'.$basePath.'/'.$imgFileName);
self::message($array);
}else{
@unlink($savepath.'/'.$imgFileName);
$array = array("error"=>1,"message"=>"图片处理失败!");
self::message($array);
}
}
public function imgResetSize($info){
$image = new \Think\Image();
$base_info = getimagesize($info['newpath']);
if(false === $base_info || (IMAGETYPE_GIF === $base_info[2] && empty($base_info['bits']))){
@unlink($info['newpath']);
$array = array("error"=>1,"message"=>"非法图像文件!");
self::message($array);
}
if($info['ext']=='gif'){
return true;
}elseif($info['ext']=='png'){
$srcImg = imagecreatefrompng($info['newpath']);
$srcWidth = imagesx($srcImg);
$srcHeight = imagesy($srcImg);
$newImg = imagecreatetruecolor($srcWidth, $srcHeight);
$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $alpha);
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
imagesavealpha($newImg, true);
return imagepng($newImg, $info['newpath']);
}
if($image->open($info['newpath'])){
return $image->save($info['newpath'], $info['ext'], 85);
}else{
$array = array("error"=>1,"message"=>"文件打开失败!");
self::message($array);
}
}
public function message($data){
echo json_encode($data);
exit();
}
public function getFileName($savepath,$type){
$imgFileName = mt_rand(0,9999999).'.'.$type;
if(file_exists($savepath.'/'.$imgFileName)){
return self::getFileName($savepath,$type);
}else{
return $imgFileName;
}
}
}
接口地址
文件上传方式
http://dgn.study.com/index.php/Home/Index/fileUpload.html
base64 方式
http://dgn.study.com/index.php/Home/Index/formUpload.html
接收方式 POST
必须参数
文件上传方式
POST.path 站点路径
FILES.imgFile 文件
base64 方式
POST.img 图片base64值
POST.type 图片类型
POST.webdir 站点路径
返回值类型 JSON
返回值
error 错误代码
1 有错误信息 失败
0 没有错误信息 成功
message 失败信息
只有失败才返回
url 图片路径
只有成功才返回