2017-10-01 3 views
0

私は、ユーザーが画像やその他の詳細などをアップロードすることを強制しています。そして、私のルートディレクトリには、(user_images)すべてのものは大丈夫ですが、Webページの読み込みが遅くなるように、問題のis..userが...データベースにアップロードする前にサイズを変更する方法 を大きなファイルを挿入することができる...ここ品質を失うことなくデータベースに保存する前にイメージのサイズを圧縮する方法

は私のコードは...

です
if(isset($_POST['btnsave'])) 
{ 
    $camname = $_POST['cam_name'];// user name 
    $modelname = $_POST['model'];// user email 
    $rentpday = $_POST['rent_pday'];// user name 
    $usermob = $_POST['mob'];// user email 
    $useraddrs = $_POST['addrs'];// user email 
    $upd_date =date('Y-m-d H:i:s');//upl_date 

    $imgFile = $_FILES['user_image']['name']; 
    $tmp_dir = $_FILES['user_image']['tmp_name']; 
    $imgSize = $_FILES['user_image']['size']; 


    if(empty($camname)){ 
     $errMSG = "Please Enter Cam name."; 
    } 
    else if(empty($usermob)){ 
     $errMSG = "Please mobile number"; 
    } 
    else if(empty($camname)){ 
     $errMSG = "Please enter cam_name"; 
    } 
    else if(empty($modelname)){ 
     $errMSG = "Please enter model"; 
    } 
    else if(empty($rentpday)){ 
     $errMSG = "Please enter rent per day"; 
    } 
    else if(empty($imgFile)){ 
     $errMSG = "Please Select Image File."; 
    } 
    else 
    { 
     $upload_dir = 'user_images/'; // upload directory 

     $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension 

     // valid image extensions 
     $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions 

     // rename uploading image 
     $userpic = rand(1000,1000000).".".$imgExt; 

     // allow valid image file formats 
     if(in_array($imgExt, $valid_extensions)){   
      // Check file size '5MB' 
      if($imgSize < 5000000)    { 
       move_uploaded_file($tmp_dir,$upload_dir.$userpic); 
      } 
      else{ 
       $errMSG = "Sorry, your file is too large."; 
      } 
     } 
     else{ 
      $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";   
     } 
    } 


    // if no error occured, continue .... 
    if(!isset($errMSG)) 
    { 


     $stmt = $user_home->runQuery("UPDATE post_data 
             set cam_name=?, 
             cam_model =?, 
             cam_rent=?, 
             cam_img=?, 
             mobile=?, 
             address=?, 
             upd_date=? 
             where userID=? 
             "); 

     $stmt->bindParam(1,$camname); 
     $stmt->bindParam(2,$modelname); 
     $stmt->bindParam(3,$rentpday); 
     $stmt->bindParam(4,$userpic); 
     $stmt->bindParam(5,$usermob); 
     $stmt->bindParam(6,$useraddrs); 
     $stmt->bindParam(7,$upd_date); 
     $stmt->bindParam(8,$id); 

     if($stmt->execute()) 
     { 

      $successMSG = "Record saved success"; 
     } 
     else 
     { 
      $errMSG = "error while inserting...."; 
     } 

    } 
} 

アップロードする前に画像サイズを圧縮するようにこのコードを変更しました。 ありがとうございます。

+0

特定のサイズに制限することができます – Ravi

+0

私は5MBに設定します...ただし、ユーザーが4.2MBイメージファイルを入力すると、何かエラーを与えて、データベースに挿入しますが、イメージファイルはWebページに表示されません。 –

答えて

0

AFAIK、画像のサイズを小さくしながら画質を維持することはできません。 imagejpegを使用すると、イメージのサイズを縮小してイメージのサイズを縮小できます。

画像を保存しようとしているときは、まず画像の品質を下げる必要があります。

 if($imgSize < 5000000) { 
      $image = imagecreatefromjpeg($upload_dir.$userpic); 
      imagejpeg($image, $new_image_path, 70); 
      move_uploaded_file($tmp_dir,$new_image_path); 
     } 
+0

oh ..私の上記に使用する場合 –

+0

@lohithkumar私の更新を確認してください – Ravi

+0

@lohithkumarまた、品質を低下させています。 – Ravi

0

実際に画像を圧縮するのは非常に簡単です。すべてをPNGに変更してください:

$image = imagecreatefromstring(file_get_contents($tmp_dir)); 
$saveLocation = “user_images/”.md5(uniqid()).”png”; 
//Please use this instead of your current file naming method to remove the possibility of a duplicate (which rand will sometimes yield) 
imagepng($image,$saveLocation,9); 
//9 represents the compression level (0 no compression - 9 max compression) 

本当にそれです! PNGは無損失圧縮方式ですので、プロセスで品質を失うことはありません

関連する問題