標籤:
<?phprequire dirname(__FILE__).‘/../includes/common.inc.php‘; $_clean = array(); $_info = array(); $_data = array(); $_clean[‘shopId‘] =_check($_POST[‘shopId‘]); $_clean[‘userId‘] =_check($_POST[‘userId‘]); $oldpic = $_FILES[‘picFile‘][‘tmp_name‘]; if ( $_clean[‘shopId‘]!=‘‘ && $_clean[‘userId‘]!=‘‘ ) { $_info[‘state‘] = 0; $_info[‘info‘] = ‘‘; $max_file_size=500000; //大小500kb if($max_file_size <$oldpic["size"]){ $_info[‘state‘] = 2; $_info[‘info‘] = ‘檔案太大‘; echo _json($_info); die; }else{ $_sql1 = "SELECT * FROM tb_easy_Comments WHERE user = ‘{$_clean[‘userId‘]}‘ and contentId = ‘{$_clean[‘contentId‘]}‘"; $_result1 = _query($_sql1); $_row1 = _num_rows($_result1); if ( $_row1 > 0 ) { $_info[‘state‘] = 0; $_info[‘info‘] = ‘您已經評論過‘; echo _json($_info); die; }else{ $width = 200; $height = 200; header(‘Content-Type: image/jpeg,image/png,image/gif‘); //擷取到圖片的寬和高 list($width_orig, $height_orig) = getimagesize($oldpic); $ratio_orig = $width_orig/$height_orig; //算比例 if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } //建立 $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefrompng($oldpic); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); $pic = date(‘YmdHis‘).rand(1000,9999).‘.‘.jpg; imagejpeg($image_p,"D:/www/web/php_easyhunt/images/easyCommentsPic/".$pic , 100); $_data[‘info‘] ="http://". $_SERVER[‘SERVER_NAME‘].‘/images/easyCommentsPic/‘.$pic; $_info[‘data‘] = $_data; echo _json($_info); } } }else{ $_data[‘state‘] = 5; $_data[‘info‘] = ‘參數有誤!‘; } $_info[‘data‘] = $_data; echo _json($_info);//請勿刪除以下內容 以後備用 // require dirname(__FILE__).‘/../includes/common.inc.php‘;// $_clean = array();// $_info = array();// $_data = array();// $_clean[‘shopId‘] =_check($_POST[‘shopId‘]);// $_clean[‘userId‘] =_check($_POST[‘userId‘]);// // $oldpic = $_FILES[‘picFile‘][‘name‘];// //上傳後的名稱// $newpic = date(‘YmdHis‘).rand(1000,9999).$oldpic; // // if ( $_clean[‘shopId‘]!=‘‘ && $_clean[‘userId‘]!=‘‘ ) {// // $_info[‘state‘] = 0;// $_info[‘info‘] = ‘‘;// $max_file_size=500000; //大小500kb// if($max_file_size <$oldpic["size"]){ // $_info[‘state‘] = 2;// $_info[‘info‘] = ‘檔案太大‘;// echo _json($_info);// die;// }// //接收檔案目錄 // $target_path = "D:/www/web/php_easyhunt/images/companyImages/"; // if(move_uploaded_file($_FILES[‘picFile‘][‘tmp_name‘], $target_path. $newpic)) { // }// $_data[‘info‘] ="http://". $_SERVER[‘SERVER_NAME‘].‘/images/companyImages/‘.$newpic; // }else{// $_data[‘state‘] = 5;// $_data[‘info‘] = ‘參數有誤!‘;//// }// // $_info[‘data‘] = $_data;// // echo _json($_info);//// $image_p = imagecreatetruecolor($width, $height);// $type = substr($_absolutePath,strrpos($_absolutePath, ‘.‘)+1); // if($type == "jpg" || $type == "JPEG"||$type == "jpeg") {// $image = imagecreatefromjpeg($_absolutePath);// } else if ($type == "png") {// $image = imagecreatefrompng($_absolutePath);// } else if ($type == "gif") {// $image = imagecreatefromgif($_absolutePath);// }////// function scaleImage(){// /*$_absolutePath="http://115.28.233.45/mobile_client".$_path;*/// $_absolutePath="http://115.28.233.45/mobile_client/imgs/comments/4_220/201412270932136d2fcd32-7764-4081-9ae5-6f85bbfadf71.JPEG";// $width = 200;// $height = 200;// header(‘Content-Type: image/jpeg,image/png,image/gif‘);// //擷取到圖片的寬和高// list($width_orig, $height_orig) = getimagesize($_absolutePath);// $ratio_orig = $width_orig/$height_orig;// //算比例// if ($width/$height > $ratio_orig) {// $width = $height*$ratio_orig;// } else {// $height = $width/$ratio_orig;// }// //建立// $image_p = imagecreatetruecolor($width, $height);// $type = substr($_absolutePath,strrpos($_absolutePath, ‘.‘)+1); // if($type == "jpg" || $type == "JPEG"||$type == "jpeg") {// $image = imagecreatefromjpeg($_absolutePath);// } else if ($type == "png") {// $image = imagecreatefrompng($_absolutePath);// } else if ($type == "gif") {// $image = imagecreatefromgif($_absolutePath);// }// imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);// $pic ="smalll_".substr($_absolutePath,strrpos($_absolutePath, ‘/‘)+1);// imagejpeg($image_p,"imgs/comments/".$pic , 100);// }?>View Code
php 圖片壓縮處理