当前位置:   article > 正文

php上传图片

php上传图片

php上传图片

	//异步图片上传
	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);
			}   
		}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号