如何在上传图像后检查图像的尺寸 如果它与我想要的尺寸不匹配,请删除它吗?
所以在挖掘之后我发现PHP无法做尺寸。我所遵循的解决方案是:
这是我的代码。有人可以告诉我如何检查当前文件的尺寸以及如果不匹配,如何删除文件夹和文件?
# 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 :(得分:6)
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;
}
答案 1 :(得分: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));
}
答案 2 :(得分:1)
获取dimensio n temp_file
非常简单易用$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
答案 3 :(得分:0)
这个怎么样? http://php.net/manual/en/function.getimagesize.php
http://php.net/manual/en/function.rmdir.php
要删除该文件夹,该文件夹应为空。