thinkphp3.2.3多图上传

/**
* 
* 上传单张图片封装方法 
* 
* $pic 上传图片
* 
* $style false 多图 单图 true
* 
*/
public function Pic($pic,$style=null){

$upload = new \Think\Upload();
$upload->maxSize = 3145728; // 设置附件上传大小
$upload->exts = array('jpg', 'png');// 设置附件上传类型
$upload->rootPath = "./";
$upload->savePath = 'uploads/Price/'; //保存路径
if($style){
$z = $upload -> uploadOne($pic);
if(!$z) {// 上传错误提示错误信息
$this->ajaxReturn(array('err'=>$upload->getError()));
}
//生成缩略图
$image = new \Think\Image(); 
$image->open($z['savepath'].$z['savename']);
$url = $z['savepath'].'icon_'.$z['savename'];
$image->thumb(100,100,6)->save($url);

$image->open($z['savepath'].$z['savename']);
$url2 = $z['savepath'].'max_'.$z['savename'];
$image->thumb(750,350,6)->save($url2);
unlink($z['savepath'].$z['savename']); 
}else{
// 上传文件 
$info = $upload->upload($pic);
if(!$info) {// 上传错误提示错误信息
$this->AjaxReturn(array('err' => $upload->getError()));
}else{// 上传成功 获取上传文件信息
$image = new \Think\Image(); 
foreach($info as $file){
$purl =$file['savepath'].$file['savename'];
$image->open( $purl );
$max=$file['savepath'].'max_'.$file['savename'];
$image->thumb(750,350,6)->save($max); 
$url .= $max.',';
unlink($purl);
}
}
$url = rtrim($url,',');

}
return $url;
}    
/**
* 转换多图上传数组格式
*/
public function Keyarr($data){
$arr = $abb = array();
$arrkey = array('name','type','tmp_name','error','size');
foreach ($data as $v) {
foreach ($v as $key => $j) {
$arr[$key][]=$j;
}
}
for ($i=0; $i < count($arr); $i++) {
$abb[] = array_combine($arrkey,$arr[$i]);
}
return $abb;
}

 

 

posted on 2017-04-13 17:19  炊厨  阅读(1231)  评论(0)    收藏  举报

导航