thinkphp上传图片并批量生成不同尺寸缩略图

时间:2022-09-24 15:59:18

1、配置信息

    //****************************各种类型图片缩略图尺寸设置*************************//
'IMAGES_THUMB_SIZE_SETTING' =>array(
'USER_HEADER' =>array( //用户头像缩略图大小
'64*64' =>'64*64',
'32*32' =>'32*32'
),
'PHOTOS_HEADER' =>'128*128', //图片缩略图大小
'EMOTIONS_PHOTO' =>'128*128' //动态缩略图大小
),

2、详细代码

/**
* 文件上传方法,TP在此处有个坑,当图片名称命名不规范时,图片不能保存而且不会提示错误
* 成功返回图片保存路径
* 失败返回相关失败信息
*/
public function upload(){
/**
* file_put_contents('1.txt',var_export($_POST,true));
* file_put_contents('2.txt',var_export($GLOBALS['HTTP_RAW_POST_DATA'],true));
* $raw_post_data = file_get_contents('php://input', 'r');
* file_put_contents('3.txt',var_export($raw_post_data, true));
* file_put_contents('4.txt',var_export($_FILES, true));
*/
$user_id = I('post.user_id',0,'intval');
$type = I('get.type',1,'intval');//操作类型,1:用户头像上传,2:图片上传,3:说说图片上传
$token = $this->token;
$data = array();
if($user_id){
//user_id有传的话,那么说明是更新用户头像操作
$return = $this->check_user_valid($token,$user_id);
if(!$return){
$data['ret_code'] = C('API_STATUS_CODE.USER_DOES_NOT_HAVE_POWER');
$data['ret_msg'] = C('API_STATUS_CODE.USER_DOES_NOT_HAVE_POWER_MSG');
$this->ajaxReturn($data);
}
}
//设置文件存放路径及缩放比例
$folder = '';
$thumb_size = '';
switch($type){
case 1:
$folder = 'header';
$thumb_size = C('IMAGES_THUMB_SIZE_SETTING.USER_HEADER');
break;
case 2:
$folder = 'photos';
$thumb_size = C('IMAGES_THUMB_SIZE_SETTING.PHOTOS_HEADER');
break;
case 3:
$folder = 'emotions';
$thumb_size = C('IMAGES_THUMB_SIZE_SETTING.EMOTIONS_PHOTO');
break;
}
$path = C('SYSTEM_IMAGES_FOLDERS').'images/'.$folder;
if(!file_exists($path)){
mkdir($path);
chmod($path,755);
}

$file_arr = explode('.',$_FILES['file']['name']);
$file_name = $file_arr[0];
$sha1 = sha1(time().mt_rand());

$config = array();
$config['maxSize'] = 3145728;
$config['exts'] = array('jpg', 'gif', 'png', 'jpeg','JPG','GIF','PNG','JPEG');
$config['rootPath'] = $path;
$config['savePath'] = '/';
$config['saveName'] = $sha1.'-'.$file_name;
$upload = new \Think\Upload($config);
$info = $upload->uploadOne($_FILES['file']);
if(!$info) {
$data['ret_code'] = C('API_STATUS_CODE.FAILED');
$data['ret_msg'] = $upload->getError();
}else{
//文件存放路径
$url = $path.$info['savepath'].$info['savename'];
//生成缩略图
$all_img_url = $this->_multi_thumb_save($path,$info,$sha1,$thumb_size,$_FILES['file']['name']);
$data['ret_code'] = C('API_STATUS_CODE.SUCCESS');
$data['ret_msg'] = C('API_STATUS_CODE.SUCCESS_MSG');
$data['data'] = array(
'url' =>$url,
'thumb_url' =>$all_img_url
);
}
$this->ajaxReturn($data);
}

/**
* 批量生成不同尺寸缩略图
* @param $path 原始图片
* @param $info 图片上传后获得的object
* @param $sha1 sha1()后得到的值
* @param $size_arr 缩略尺寸数组
* @param $file_name 原始图片名称
* @return string 所有缩略图地址字串
*/
private function _multi_thumb_save($path,$info,$sha1,$size_arr,$file_name){
$all_img_url = '';
$img_url_arr = array();
//需要生成多个缩略图
if(is_array($size_arr)){
foreach($size_arr as $key =>$value){
$img_url_arr[$key] = $this->_thumb_save($path,$info,$sha1,$value,$file_name);
}
$all_img_url = json_encode($img_url_arr);
}else{
$all_img_url = $this->_thumb_save($path,$info,$sha1,$size_arr,$file_name);
}
return $all_img_url;
}

/**
* 批量生成不同尺寸缩略图
* @param $path 原始图片
* @param $info 图片上传后获得的object
* @param $sha1 sha1()后得到的值
* @param $thumb_size 缩略尺寸
* @param $file_name 原始图片名称
* @return string 所有缩略图地址字串
*/
private function _thumb_save($path,$info,$sha1,$thumb_size,$file_name){
$source = $path.$info['savepath'].$info['savename'];
$image = new \Think\Image();
$image->open($source);
$size = explode('*',$thumb_size);
$width = $size[0];
$height = $size[1];
//如果原始图片尺寸本来就小于缩略尺寸,那么不做压缩操作
if($image->width() <= $width && $image->height() <= $height){
$name = $source;
}else{
$name = $path.$info['savepath'].$sha1.'-'.$width.'x'.$height.'-'.$file_name;
$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_SCALE);
$image->save($name);
}
return $name;
}