2012-02-16 163 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];