2017-10-19 111 views
1

目前有一個簡單的功能,但它允許用戶添加大圖像。我希望讓所有的圖片上傳,但應該有,直到某些文件的大小滿足(如在200KB例如))命題調整圖像大小,直到符合所需的文件大小

if($_FILES['file']['size'] != 0){ 

     $uploadOk = 0; 

     //image object 
     $image = $_FILES['file']; 

     //create unique name for file 
     $imageRandomName = substr(md5(time()), 0, 5) . "-" . $image['name']; 

     //upload dir 
     $target_dir = "rest/user" . $_SESSION["username"] . "/events/" . $_SESSION["event_id"] . "/people/"; 

     //make folder for user if non exists 
     if(!file_exists($target_dir)){ 
      mkdir($target_dir, 0777, true); 
     } 

     //file to be upload 
     $target_file = $target_dir . base($imageRandomName); 

     //get file type 
     $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 

     $check = getimagesize($image["tmp_name"]); 

     if($check !== false){ 
      $uploadOk = 1; 

     } else { 
      $uploadOk = 0; 
     } 

     if($uploadOk == 0){ 

      echo "noupload"; 

     } else { 

      if (move_uploaded_file($image["tmp_name"], $target_file)) { 

       //get file size on server 
       $filesizeonserver = filesize($target_file); 
       echo "file: ".$target_file." size: ".$filesizeonserver; 
       $times = 0; 
       if($filesizeonserver > 100000){ 
        do{ 
         $resized = resizeImage($target_file, "0.1"); 
         $filesizeonserver = filesize($target_file); 
         $times++;  
        } while ($filesizeonserver > 100000); 
        echo "resized ".$times." times"; 
       } else { 

       } 


       //mysql query 

      } else { 

       echo "Sorry, there was an error uploading your file."; 

      } 
     } 
    } else { 
     //do mysql query 
    } 

resizeImage(

function resizeImage($file, $percent){ 
    list($width, $height) = getimagesize($file); 
    $newwidth = $width-($width*$percent); 
    $newheight = $height-($height*$percent); 
    $thumb = imagecreatetruecolor($newwidth, $newheight); 
    $source = imagecreatefromjpeg($file); 
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
    $newImage = imagejpeg($thumb, $file, 100); 
    return $newImage; 
} 

現在調整圖像大小的功能我我想我真的很接近實現它,但由於一些奇怪的原因,我得到的圖像尺寸爲1px寬,4px高,尺寸爲4kb。完全黑色的圖像。

如果你在想什麼圖像時增加了這個功能: 1232055字節,1220×1829,爲72dpi,JPEG

編輯:好的,現在我覺得有什麼不妥的resizeImage功能。因爲它只在第一次運作。出於某種原因,它無法獲得圖像,並呈現出黑色和微小。

編輯2:現在我正在獲取圖像大小調整功能,它沒有任何問題。我注意到該函數不斷調整圖像的大小直到其結束。我可以看到它如何縮小。

編輯3:我找到了問題。現在看來,$ filesizeonserver始終是第一個值,並在做...而它不更新,即使你是我設置它改變。

+0

要限制文件大小上傳嗎?你可以簡單地檢查 $ _FILES ['file'] ['size'] –

+0

你已經顯示的代碼片段開始於_檢查上傳文件的大小是否不是0,並且你不能拿出任何東西? – CBroe

+0

當你明白你不明白這一點時,立即對問題進行評級是非常不合理的。我需要的是調整圖像大小的函數,我不知道如何繼續。 –

回答

0
$allowedFileTypes = ['image/png', 'image/gif', 'image/jpeg', 'image/jpg']; 

     if (in_array($_FILES['image']['type'], $allowedFileTypes)) { 
      $imageType = $_FILES['image']['type']; 
      //allow file size under 10mb 
      if($_FILES['file']['size'] < 10000000){ 

       //image object 
       $image = $_FILES['file']; 

       //so there is no same name files 
       $imageRandomName = substr(md5(time()), 0, 5) . "-" . $image['name']; 

       //place where image is uploaded 
       $target_dir = 'rest/user/' . $_SESSION['username'] . '/events/' . $_SESSION['event_id'] . '/people/'; 

       //make folder for users 
       if (!file_exists($target_dir)) { 
        mkdir($target_dir, 0777, true); 
       } 

       //file to be uploded 
       $target_file = $target_dir . basename($imageRandomName); 

       //get file type 
       //$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 

       $check = getimagesize($image["tmp_name"]); 

       if ($check !== false) { 
        echo "File is an image - " . $check["mime"] . "."; 
        $uploadOk = 1; 
       } else { 
        echo "File is not an image."; 
        $uploadOk = 0; 
       } 

       //check if upload is ok 
       if ($uploadOk == 0) { 
        echo "Sorry cant upload"; 
       } else { 
        if (move_uploaded_file($image["tmp_name"], $target_file)) { 


         //get file size on server 
         $filesizeonserver = filesize($target_file); 

         $times = 0; 

         if($filesizeonserver > 300000){ 
          do{ 
           clearstatcache(); 
           $resized = resizeImage($target_file, 0.05, $imageType); 
           $filesizeonserver = filesize($target_file); 
           $times++;  
          } while ($filesizeonserver > 300000); 
          echo "resized ".$times." times"; 
         } else { 

         } 

         //sql query 

        } else { 
         echo "Sorry, there was an error uploading your file."; 
        } 
       } 

      } else { 
       //sql query 
      } 
     } else { 
      echo "wrongimagetype"; 
     } 

resizeImage() 

function resizeImage($file, $percent, $imageType){ 

    list($width, $height) = getimagesize($file); 
    $newwidth = $width-($width*$percent); 
    $newheight = $height-($height*$percent); 

    $thumb = imagecreatetruecolor($newwidth, $newheight); 

    switch($imageType){ 
     case 'image/png': 
      $background = imagecolorallocate($thumb, 0, 0, 0); 
      imagecolortransparent($thumb, $background); 
      imagealphablending($thumb, false); 
      imagesavealpha($thumb, true); 
      $source = imagecreatefrompng($file); 
      break; 
     case 'image/gif': 
      $background = imagecolorallocate($thumb, 0, 0, 0); 
      imagecolortransparent($thumb, $background); 
      imagealphablending($thumb, false); 
      imagesavealpha($thumb, true); 
      $source = imagecreatefromgif($file); 
      break; 
     case 'image/jpeg': 
     case 'image/jpg': 
      $source = imagecreatefromjpeg($file); 
      break; 
    } 

    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    switch($imageType){ 
     case 'image/png': 
      $image = imagepng($thumb, $file, 0); 
      break; 
     case 'image/gif': 
      $image = imagegif($file); 
      break; 
     case 'image/jpeg': 
     case 'image/jpg': 
      $image = imagejpeg($thumb, $file, 100); 
      break; 
    } 

    return $image; 
} 

OUTPUT:

File is an image - image/jpeg.resized 17 times 

免責聲明:所有的東西你作品的圖像質量受些影響。需要努力。

+0

對問題的最後評論是通過在調整大小之前追加clearstatcache()來解決的。 –

+0

添加allowedFileTypes png,gif,jpg和jpeg。 –

+0

現在它支持透明度,圖像質量不匹配Photoshop:D –