[英]PHP Image Upload Checking Dimensions
如何在上傳圖片后檢查圖片的尺寸,如果圖片與我想要的尺寸不符,請將其刪除?
所以在挖掘后我發現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;
}
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;
}
我在GD庫中使用getimagesize函數。
用法示例:
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));
}
使用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.