tp3.2 自带的文件上传及生成缩略图功能

时间:2023-03-10 02:31:14
tp3.2 自带的文件上传及生成缩略图功能
 public function upload_file($file_name,$width,$height) {
//检查图片尺寸是否合法
$image_size = getimagesize($_FILES[$file_name]['tmp_name']);
$img_tmp_width=$image_size[''];
$img_tmp_height=$image_size[''];
$size_result = $this->checkImgSize($width,$height,$img_tmp_width,$img_tmp_height);
if($size_result['status'] == ''){
return $size_result; //格式错误直接返回
}
//执行上传
$upload_path = C('upload_path'); // Public/Uploads/
$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = ; // 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
$upload->rootPath = './' . rtrim($upload_path, '/'); // 设置附件上传根目录
$upload_img_url=C('upload_img_url'); // /www/web/feelee_mall_img/public_html/
$rootPath=$upload_img_url . rtrim($upload_path, '/'); // 设置附件上传根目录 /www/web/feelee_mall_img/public_html/Public/Uploads
$upload->rootPath = $rootPath;
$savepath = '/ad/';
$path = '/' . $upload_path;
$upload->saveName = uniqid();
$upload->savePath = $savepath;
$upload->replace = true;
$upload->autoSub = true;
$upload->subName = "origin"; //date("Ymd");
$path1='/ad/origin/';
if(!is_dir($path1)){
mkdir($path1,);
}
// 上传单个文件
$info = $upload->uploadOne($_FILES[$file_name]);
if (!$info) {// 上传错误提示错误信息
$upload_error = C('upload_error_msg');
$error = $upload_error[$upload->getError()];
if ($error == '') {
$error = $upload->getError();
}
return $data = array(
'msg' => $error,
'status' => ,
'result'=>null
);
} else {// 上传成功 获取上传文件信息
$filenames = $path . $info['savepath'] . $info['savename'];
//生成缩略图
$info2=$this->createThumb($info,$rootPath);
$preview=C('img_base').$filenames;
return $data = array(
'msg' => '上传成功',
'status' => '',
'result' =>array(
'returnPath'=>$filenames,//保存用
'preview'=>$preview //显示用
)
);
}
}
//生成缩略图
public function createThumb($info,$rootPath){
$path2=$rootPath.'/ad/thumb/';
if(!is_dir($path2)){
mkdir($path2,);
}
$pic_size=C('pic_size');
$cn=count($pic_size);
$image=new \Think\Image();
//打开要生成缩略图的文件
for($i=;$i<$cn;$i++){
$image->open($rootPath."/ad/origin/".$info['savename']);
$url_pic='/thumb/'.$pic_size[$i] ."_". $info['savename'];
$in=strpos($pic_size[$i],"_");
$width=substr($pic_size[$i],,$in);
$height=substr($pic_size[$i],$in+);
//生成ios缩略图
$image->thumb($width,$height,)->save($rootPath."/ad".$url_pic);
}
}

manage\Application\Common\Conf\config.php

 'upload_img_url'=>'/home/wwwroot/default/feelee_mall_img/public_html/',
//文件上传返回错误说明替换
"upload_error_msg" =>array(
'没有上传的文件!' => '没有上传的文件!',
'非法图像文件!' => '不允许的文件类型,只能上传jpg,gif,png,jpeg格式的文件!',
'未知上传错误!' => '未知上传错误!',
'非法上传文件!' => '非法上传文件!',
'上传文件大小不符!' => '上传文件大小超过3M!',
'上传文件MIME类型不允许!' => '不允许的文件类型,只能上传jpg,gif,png,jpeg格式的文件!',
'上传文件后缀不允许' => '不允许的文件类型,只能上传jpg,gif,png,jpeg格式的文件!',
'上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!' => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!',
'上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!' => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!',
'文件只有部分被上传!' => '文件只有部分被上传!',
'没有文件被上传!' => '没有文件被上传!',
'找不到临时文件夹!' => '找不到临时文件夹!',
'文件写入失败!' => '文件写入失败!',
'文件命名规则错误!' => '文件命名规则错误!'
),

//图片缩略图尺寸
'pic_size'=>array("640_960","640_1136","750_1334","1242_2208","1125_2436","720_1280","800_1280","1080_1920","1440_2560"),