2014-09-06 68 views
-5

我有一個圖像和視頻上傳.. 我嘗試上傳52個圖像文件的大小爲1.38 Mb的大小 ,但當檢查它在我的服務器上的大小,我發現它的5.2 Mb .. 這是php上傳的代碼..我可以知道問題在哪裏嗎?爲什麼上傳者在php ..增加文件大小?

<?php 
    if (isset($_FILES['upload_img'])) { 
     $uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/upload/img/'; //path you wish to store you uploaded files 
     echo '<h2>Image(s) Uploaded</h2><ul style="overflow: scroll;height: 200px;">'; 
     //Loop through each file 
     for($i = 0; $i < count($_FILES['upload_img']['name']); $i++) { 
      //Get the temp file path 
      $tmpFilePath = $_FILES['upload_img']['tmp_name'][$i]; 

      //Make sure we have a filepath 
      if ($tmpFilePath != ""){ 
       //Setup our new file path 
       $uploadedFile = $uploadDir . basename($_FILES['upload_img']['name'][$i]); 

       //Upload the file into the temp dir 
       $dir = pathinfo($uploadedFile,PATHINFO_DIRNAME); 
       $actual_name = pathinfo($uploadedFile,PATHINFO_FILENAME); 
       $original_name = $actual_name; 
       $extension = pathinfo($uploadedFile, PATHINFO_EXTENSION); 
       $j = 1; 
       while(file_exists($uploadedFile)) 
       {   
        $actual_name = (string)$original_name.$j; 
        $uploadedFile = $dir."/".$actual_name.".".$extension; 
        $j++; 
       } 

       $size = getimagesize($tmpFilePath); 
       $ratio = $size[0]/$size[1]; // width/height 

       if($ratio > 1) { 
        $width = 300; 
        $height = 300/$ratio; 
       } 
       else { 
        $width = 300*$ratio; 
        $height = 300; 
       } 
       $src = imagecreatefromstring(file_get_contents($tmpFilePath)); 
       $dst = imagecreatetruecolor($width,$height); 
       imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
       imagedestroy($src); 
//    if(move_uploaded_file($tmpFilePath, $uploadedFile)) {    
       if(imagepng($dst,$uploadedFile)) { 
        $fpath = '/upload/img/' . $actual_name.".".$extension; 
        echo '<li style="list-style:none">' . $fpath . '</li>'; 
        //save to db 
        save_db_img($fpath, $_POST['cat_value_img']); 
       } else { 
        echo '<h2>There was a problem saving the uploaded file</h2>'; 
       } 
       imagedestroy($dst); 
      } 
     } 
     echo '</ul>'; 
    } 

    if (isset($_FILES['upload_video'])) { 
     $uploadDir = $_SERVER['DOCUMENT_ROOT'] . '/upload/video/'; //path you wish to store you uploaded files 
     echo '<h2>Video(s) Uploaded</h2><ul style="overflow: scroll;height: 200px;">'; 
     //Get the temp file path 
     $tmpVideoPath = $_FILES['upload_video']['tmp_name']; 
     $tmpImgPath = $_FILES['upload_timg']['tmp_name']; 

     //Setup our new file path 
     $uploadedVideoFile = $uploadDir . basename($_FILES['upload_video']['name']); 
     $uploadedImgFile = $uploadDir . basename($_FILES['upload_timg']['name']); 

     $dir = pathinfo($uploadedVideoFile,PATHINFO_DIRNAME); 
     $actual_name = pathinfo($uploadedVideoFile,PATHINFO_FILENAME); 
     $original_name = $actual_name; 
     $extension = pathinfo($uploadedVideoFile, PATHINFO_EXTENSION); 
     $j = 1; 
     while(file_exists($uploadedVideoFile)) 
     {   
      $actual_name = (string)$original_name.$j; 
      $uploadedVideoFile = $dir."/".$actual_name.".".$extension; 
      $j++; 
     } 

     $dir_img = pathinfo($uploadedImgFile,PATHINFO_DIRNAME); 
     $actual_name_img = pathinfo($uploadedImgFile,PATHINFO_FILENAME); 
     $original_name_img = $actual_name_img; 
     $extension_img = pathinfo($uploadedImgFile, PATHINFO_EXTENSION); 
     $k = 1; 
     while(file_exists($uploadedImgFile)) 
     {   
      $actual_name_img = (string)$original_name_img.$k; 
      $uploadedImgFile = $dir."/".$actual_name_img.".".$extension_img; 
      $k++; 
     } 

     //Upload the file into the temp dir 
     if(move_uploaded_file($tmpVideoPath, $uploadedVideoFile)) {    
      $fvideo_path = '/upload/video/' . $actual_name.".".$extension; 
      echo '<li style="list-style:none">' . $fvideo_path . '</li>'; 

       $size = getimagesize($tmpImgPath); 
       $ratio = $size[0]/$size[1]; // width/height 

       if($ratio > 1) { 
        $width = 300; 
        $height = 300/$ratio; 
       } 
       else { 
        $width = 300*$ratio; 
        $height = 300; 
       } 
       $src = imagecreatefromstring(file_get_contents($tmpImgPath)); 
       $dst = imagecreatetruecolor($width,$height); 
       imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]); 
       imagedestroy($src); 
       if(imagepng($dst,$uploadedImgFile)) { 
        $fimg_path = '/upload/video/' . $actual_name_img.".".$extension_img; 
        echo '<li style="list-style:none"> with Image Title of ' . $fimg_path . '</li>'; 
       } 
       imagedestroy($dst); 
      //save to db 
      save_db_video($fvideo_path, $fimg_path, $_POST['cat_value_video'], $_POST['video_title']); 
     } else { 
      echo '<h2>There was a problem saving the uploaded file</h2>'; 
     } 

     echo '</ul>'; 

    } 

    echo '<button class="btn_back"><a href="members.php"><h2>Back to Uploader</h2></a></button>'; 
+1

請不要只是轉儲所有的代碼,而是嘗試做一個[簡短,自包含,正確(可編譯),例子](http://sscce.org/)。例如。沒有理由在這個問題上放任何CSS。 – Beat 2014-09-06 10:22:22

+0

對不起,但我不知道問題究竟在哪裏..因爲這我把它全部 – user3717237 2014-09-06 10:23:31

+1

做一些調試,以找出問題出在哪裏,他們編輯的問題,添加該信息並減少代碼的長度 – Clive 2014-09-06 10:24:51

回答

0

嘗試調用時imagepng最大創建新的PNG和使用filters時設置的壓縮級別。

imagepng($dst, $uploadedFile, 9, PNG_ALL_FILTERS) 

可能舊圖像比您創建的新圖像更加壓縮,因此它更大。原始圖像是否也是PNG?如果它們是另一種格式,尤其是非無損格式,則可能無法防止文件大小的增加。

+0

上傳的圖像是png和jpg – user3717237 2014-09-19 07:29:44