赞
踩
//异步图片上传 public function uploads(){ // 允许上传的图片后缀 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); // echo $_FILES["file"]["size"]; $extension = end($temp); // 获取文件后缀名 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20480000) // 小于 20M,这个自己限制 && in_array($extension, $allowedExts)) { $code = $_FILES['file'];//获取小程序传来的图片 $uploaded_file=$_FILES['file']['tmp_name']; $user_path=$_SERVER['DOCUMENT_ROOT'].'/static/ad'; //放到服务器下指定的文件夹 if(file_exists($user_path)){ }else{ mkdir($user_path,0777); } $size=$_FILES["file"]["size"]; $date=date('Ymd'); //得到当前时间 $newfilename=$date.'-'.$size.'.'.$extension; //得到一个新的文件名,可根据自己需求设定,sham用的时间加上图片文件大小来命名 $move_to_file=$user_path."/".$newfilename; move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file)); //下面的代码是用来生成缩略图的 $thump = $user_path."/thumb/"; //这个缩略图文件夹地址自己设置,这个是在原图文件夹里面增加1个子目录thumb用于存放缩略图 if(file_exists($thump)){ }else{ mkdir($thump,0777); } $imgs = $newfilename; $imgss=$user_path."/".$imgs; $img_arr = getimagesize($imgss); $pecent = $img_arr[0]/$img_arr[1]; $width = 200; //这里是缩略图的尺寸,自行设置 $height = 200/$pecent; //下面是根据不同图片后缀,执行不同的图片生成代码 if($_FILES["file"]["type"] == "image/png"){ $img_in = imagecreatefrompng($imgss); }elseif($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg"){ $img_in = imagecreatefromjpeg($imgss); }elseif($_FILES["file"]["type"] == "image/gif"){ $img_in = imagecreatefromgif($imgss); } $img_out = imagecreatetruecolor($width, $height); imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $width, $height, $img_arr[0], $img_arr[1]); imagejpeg($img_out,$thump.$imgs,100); imagedestroy($img_out); imagedestroy($img_in); $imgsrc="/static/ad/".$newfilename; $thumbsrc="/static/ad/thumb/".$newfilename; $data=array( 'big'=>$imgsrc, 'thumb'=>$thumbsrc ); echo json_encode($data); }else { $data=array(); echo json_encode($data); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。