简体   繁体   English

PHP图像上传检查尺寸

[英]PHP Image Upload Checking Dimensions

How can I check the dimensions of an image after it has uploaded and delete it if it does not match the dimensions I want? 如何在上传图片后检查图片的尺寸,如果图片与我想要的尺寸不符,请将其删除?

So after digging around I find PHP cannot do dimensions. 所以在挖掘后我发现PHP无法做尺寸。 The solution I am following is: 我遵循的解决方案是:

  1. Upload the file to the server 将文件上传到服务器
  2. Use that new string and check 使用新字符串并检查
  3. Delete it or continue with upload if it does not match width and height 如果它与宽度和高度不匹配,请将其删除或继续上传

This is my code. 这是我的代码。 Can someone show me how to check the current file for dimensions and how to delete the folder and file if not matching? 有人可以告诉我如何检查当前文件的尺寸以及如果不匹配如何删除文件夹和文件?

# 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;
    }
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;
}

I use the getimagesize function with GD library. 我在GD库中使用getimagesize函数。

Example usage: 用法示例:

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));
}

For getting dimensio n Very Simple and easy with temp_file 使用temp_file 获取非常简单和简单的尺寸

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM