图像上载表单维度验证

时间:2017-05-27 07:47:45

标签: php

先生是否可以检查上传的图片尺寸匹配此尺寸250,468x60,320x50,216x36,168x28,300x250,300x50

如果不匹配则会出现错误消息

图片尺寸无效,可用尺码为300x250,468x60,320x50,216x36,168x28,300x250,300x50

2 个答案:

答案 0 :(得分:1)

查看此代码: -

$file = $_FILES["files"]['tmp_name']; // uploaded files
list($width, $height) = getimagesize($file);
$string = $width.'x'.$height;
$predefined_sizes = array('300x250', '468x60', '320x50', '216x36', '168x28', '300x250', '300x50'); // custom image size array
if(!in_array($string,$predefined_sizes)) {
    echo "Image Size is not valid, available sizes are 300x250, 468x60, 320x50, 216x36, 168x28, 300x250, 300x50";
    // Or you can also do to avoid repetion of sizes.
    //echo "Image Size is not valid, available sizes are ".implode(', ',$predefined_sizes);
    exit;
}else{
    echo 'Valid image'; 
}

答案 1 :(得分:0)

$file=$uploaddir . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {

    $size = getimagesize($files);

    $maxWidth = 500;  // as per your max width requerment 
    $maxHeight = 250; // as per your max hight requerment 

    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
        echo $result= "Image Size is not valid, available sizes are 300x250, 468x60, 320x50, 216x36, 168x28, 300x250, 300x50";
    end if
} else {

   echo $result= "Not valid Image found";
}