2012-02-16 120 views
2

如何檢查圖片上傳後的圖片尺寸,如果圖片尺寸與我想要的尺寸不匹配,則將其刪除 ?PHP Image Upload檢查尺寸

因此,經過挖掘,我發現PHP不能做尺寸。我下面的解決方案是:

  1. 將文件上傳到服務器
  2. 使用新的字符串和檢查
  3. 刪除或繼續上傳,如果它 不匹配的寬度和高度

這是我的代碼。有人能告訴我如何檢查當前文件的尺寸以及如何刪除文件夾和文件,如果不匹配?

# create our temp dir 
    mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true); 
    # upload dir settup 
    $uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/'; 
    $file=$uploaddir . basename($_FILES['file']['name']); 

    # upload the file first 
    if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) { 
     # ok so check the height and width 
     # if it is not the width and height assigned delete image and folder 
     if (image height= and width =){ 
      unlink($file); 
      rmdir($uploaddir); 
      $result=0; 
     } else { 
     # image matches height and width message ok 
      $result=1; 
     } 
    } else { 
     # error uploading 
     $result=$errormsg; 
    } 
+0

請編輯您的帖子開頭解釋你在做什麼試圖去做。不要讓我們試圖弄清楚你的目標是什麼。 – 2012-02-16 19:36:54

+1

你是什麼意思_So挖掘後我發現PHP不能做dimensions_?那麼'getimagesize()'http://php.net/manual/en/function.getimagesize.php – elclanrs 2012-02-16 19:38:30

+0

編輯..我不知道getimagesize - 它不會刪除圖像文件夾。你能告訴我一個例子嗎? – TheBlackBenzKid 2012-02-16 19:39:17

回答

5
mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true); 
# upload dir settup 
$uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/'; 
$file=$uploaddir . basename($_FILES['file']['name']); 

# upload the file first 
if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) { 
    # ok so check the height and width 
    # if it is not the width and height assigned delete image and folder 
    $size = getimagesize($files); 
    $maxWidth = 500; 
    $maxHeight = 500; 
    if ($size[0] > $maxWidth || $size[1] > $maxHeight) 
    { 
     unlink($file); 
     rmdir("./uploads/temp/".$user."/".$mx."/".$hash."/"); 
     rmdir("./uploads/temp/".$user."/".$mx."/"); 
     rmdir("./uploads/temp/".$user."/"); 
    } 
    else 
     $result=1; 
    end if 
} else { 
    # error uploading 
    $result=$errormsg; 
} 
+0

啊......你看我幾乎在那裏,因爲我只是更新了我的代碼。所以你必須刪除所有的dirs? – TheBlackBenzKid 2012-02-16 19:48:34

+0

爲了刪除它,目錄必須是空的,請參閱http://php.net/manual/en/function.rmdir。php – derekaug 2012-02-16 19:49:30

+1

在計劃移動它們之前,您應該檢查圖像尺寸。如果尺寸不正確,請不要移動它們,它們將在腳本終止時被刪除。 – thedeveloper3124 2013-08-07 23:27:46

3

我使用getimagesize功能與GD庫。

用法示例:

list($width, $height, $type, $attr) = @getimagesize($imageUri); 

if (($height > 500) && ($width > 500)) { 
    throw new Exception(sprintf('Invalid image: %d(h) and %d(w) are not within tolerable range', $height, $width)); 
} 
+0

我更新了我的代碼,請你可以告訴我如何使用IF高度是幾何和寬度使用get函數的一些東西.. – TheBlackBenzKid 2012-02-16 19:46:21

+0

其中一些內容需要您自行解決,即閱讀與發佈的解決方案相關的文檔。更新後的示例用法。 – 2012-02-16 19:50:54

+0

欣賞它。我複製了你。我檢查圖書館,我無法找到一個例子,並讓我的頭在這一個。 – TheBlackBenzKid 2012-02-16 19:54:18

1

對於越來越dimensioñ非常簡單和容易與temp_file

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]); 
$image_width = $image_info[0]; 
$image_height = $image_info[1];