[英]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: 我遵循的解决方案是:
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];
What about this? 那这个呢? http://php.net/manual/en/function.getimagesize.php http://php.net/manual/en/function.getimagesize.php
http://php.net/manual/en/function.rmdir.php http://php.net/manual/en/function.rmdir.php
To delete the folder, it should be empty. 要删除该文件夹,它应为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.