Hak*_*kan 24 php file-upload image dimensions
如何使用PHP在上传图像之前检查高度和宽度.
我必须先上传图片并使用"getimagesize()"吗?或者我可以在使用PHP上传之前检查一下吗?
<?php
foreach ($_FILES["files"]["error"] as $key => $error) {
if(
$error == UPLOAD_ERR_OK
&& $_FILES["files"]["size"][$key] < 500000
&& $_FILES["files"]["type"][$key] == "image/gif"
|| $_FILES["files"]["type"][$key] == "image/png"
|| $_FILES["files"]["type"][$key] == "image/jpeg"
|| $_FILES["files"]["type"][$key] == "image/pjpeg"
){
$filename = $_FILES["files"]["name"][$key];
if(HOW TO CHECK WIDTH AND HEIGHT)
{
echo '<p>image dimenssions must be less than 1000px width and 1000px height';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
Tha*_*nga 96
我们可以轻松地使用临时文件.
$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
Run Code Online (Sandbox Code Playgroud)
这就是我解决它的方式.
$test = getimagesize('../bilder/' . $filnamn);
$width = $test[0];
$height = $test[1];
if ($width > 1000 || $height > 1000)
{
echo '<p>iamge is to big';
unlink('../bilder/'.$filnamn);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这对我有用
$file = $_FILES["files"]['tmp_name'];
list($width, $height) = getimagesize($file);
if($width > "180" || $height > "70") {
echo "Error : image size must be 180 x 70 pixels.";
exit;
}
Run Code Online (Sandbox Code Playgroud)
如果文件在$_FILES数组中(因为它是以Multipart形式选择的),它已经被上传到服务器(通常是/ tmp或类似的文件路径),所以你可以继续使用getimagesize()php中的函数来获取尺寸(包括所有细节为数组).
小智 5
要获取图像的宽度和高度getimagesize(path_name),该函数返回包含高度、宽度、image_type 常量和其他图像相关信息的数组。通过以下代码,您可以实现这一目标。
注意- 需要传递图像的临时位置,并在使用之前使用以下代码move_upload_file(),否则它将文件移动到目标路径并且您不会获得图像所需的结果
$imageInformation = getimagesize($_FILES['celebrity_pic']['tmp_name']);
print_r($imageInformation);
$imageWidth = $imageInformation[0]; //Contains the Width of the Image
$imageHeight = $imageInformation[1]; //Contains the Height of the Image
if($imageWidth >= your_value && $imageHeight >= your_value)
{
//Your Code
}
Run Code Online (Sandbox Code Playgroud)