首頁 >後端開發 >php教程 >win7工作列縮圖 圖片上傳製作縮圖--類似手機相冊

win7工作列縮圖 圖片上傳製作縮圖--類似手機相冊

WBOY
WBOY原創
2016-07-28 08:30:14958瀏覽
function uploadPic(){
	$back_code = array();
	$upTypes = array(
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/gif',
			'image/bmp',
	);
	$temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
	$max_file_size = 10000000; // 文件大小限制
	$upload_dir = "/var/www/html/dove.kmapp.cn/Upload/WeddingSpace/"; // 上传路径
	$date_dir = date("Y-m-d",time()); // 按日期分目录存储
	$update_time = date("Y-m-d H:i:s",time()); // 按日期分目录存储
	$conn = connect_to_db();

	// 判断文件类型和文件大小
	if(in_array($_FILES['file']['type'], $upTypes) && $_FILES['file']['size'] < $max_file_size){
		if($_FILES[&#39;file&#39;][&#39;error&#39;] > 0){
			$back_code['num'] = -2;
			$back_code['msg'] =  $_FILES["file"]["error"] ;
		}else{

			// 获取手机照片的旋转角信息  1:0°,6:顺90°,8:逆90°,3:180°
			$exif_arr = exif_read_data($_FILES['file']['tmp_name'], 'IFD0');
			$Orientation = $exif_arr['Orientation'];
			if($Orientation == 1){
				$degrees = 0;
			}else if($Orientation == 6){
				$degrees = 270;
			}else if($Orientation == 8){
				$degrees = 90;
			}else if($Orientation == 3){
				$degrees = 180;
			}

			$photo_postfix = explode('/', $_FILES['file']['type'])[1];
			$randStrArr = getUrl();
			$photo_name = $randStrArr['randStr'] . $randStrArr['timeStr'] . '.' . $photo_postfix; // 文件名
			$thumb_name = 'thumb_' . $photo_name ;
			$photo_url = $date_dir . '/' . $photo_name;
			$thumb_url = $date_dir . '/' . 'thumb_' . $photo_name ;
			// 判断是否存在目录并创建
			if(!is_dir($upload_dir . $date_dir)){
				mkdir($upload_dir . $date_dir);
			}

			// 制作缩略图 *****
			$size_wh = getimagesize($_FILES['file']['tmp_name']);//{0: 1280, 1: 800, 2: 2, 3: "width="1280" height="800"", bits: 8, channels: 3, mime: "image/jpeg"}
			$thumb_width = 300;
			$thumb_height = 300;
			$size_width = $size_wh[0]; // 宽
			$size_height = $size_wh[1]; // 高
			$img_type = $size_wh[2]; // 文件类型

	 		$create_name = 'imagecreatefrom'. $temp[$img_type];
	 		$output_name = 'image' . $temp[$img_type];
	        // $cimg = imagecreatetruecolor($thumb, $thumb);
	        $timg = imagecreatetruecolor(300,300);
	        $source = $create_name($_FILES['file']['tmp_name']);

	        $ratio_w=1.0 * $thumb_width / $size_width;  
			$ratio_h=1.0 * $thumb_height / $size_height;  
			$ratio=1.0; 

			if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)){  
				$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;  
				$tmp_w = (int)($thumb_width / $ratio);  
				$tmp_h = (int)($thumb_height / $ratio);  
				$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);  
				$src_x = (int) (($size_width-$tmp_w)/2) ;  
				$src_y = (int) (($size_height-$tmp_h)/2) ;     
				imagecopy($tmp_img, $source, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);     
				imagecopyresampled($timg,$tmp_img,0,0,0,0,$thumb_width,$thumb_height,$tmp_w,$tmp_h);  
				imagedestroy($tmp_img);  
			}else{  
				$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;  
				$tmp_w = (int)($size_width * $ratio);  
				$tmp_h = (int)($size_height * $ratio);  
				$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);  
				imagecopyresampled($tmp_img,$source,0,0,0,0,$tmp_w,$tmp_h,$size_width,$size_height);  
				$src_x = (int)($tmp_w - $thumb_width) / 2 ;  
				$src_y = (int)($tmp_h - $thumb_height) / 2 ;     
				imagecopy($timg, $tmp_img, 0,0,$src_x,$src_y,$thumb_width,$thumb_height);  
				imagedestroy($tmp_img);  
			}
			
	        // if($thumb_height > $thumb_width){
	        // 	$new_height = 0;
	        // 	$new_width = 
	        // }
	        // imagecopyresampled($cimg, $source, 0,0, 0,0, $thumb,$thumb, $size_width,$size_height);
	        // imagecopyresampled($timg, $cimg, 0,0, 0,0, 120,120, $thumb,$thumb);
	        // imagecopyresampled($timg, $source, 0,0, $tw,$th, 240,240, $size_width,$size_height);

	        $rotate = imagerotate($timg, $degrees, 0);

	        if($output_name($rotate , $upload_dir . $date_dir . '/' . $thumb_name , 100)){
	        	// 上传文件
				if(move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir . $date_dir . '/' . $photo_name)){
					$insert_sql = "INSERT INTO `wedding_space_photo` (openid,wedding_ID,big_photo,small_photo,update_time) VALUES ('$openid','$wedding_ID','$photo_url','$thumb_url','$update_time') ";
					mysql_query($insert_sql);
					$id = mysql_insert_id();
					$back_code['num'] = 0;
					$back_code['msg'] = "上传成功";
					$back_code['img_id'] = $id;
					$back_code['src'] = "http://dove.kmapp.cn/Upload/WeddingSpace/".$date_dir. '/' .$photo_name;
					$back_code['thumb_src'] = "http://dove.kmapp.cn/Upload/WeddingSpace/".$date_dir. '/' .$thumb_name;
				}else{
					$back_code['num'] = -4;
					$back_code['msg'] = "上传失败,请重试";
				}
	        }else{
				$back_code['num'] = -5;
				$back_code['msg'] = "做略图制作失败,请重试";
			}				
			
		}
	}else{
		$back_code['num'] = -3;
		$back_code['msg'] = "上传文件非法";
	}
	return $back_code;
}

以上就介紹了win7任務欄縮圖 圖片上傳製作縮圖--類似手機相冊,包括了win7任務欄縮圖方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn