在使用 PHP 上传图像之前检查图像尺寸(高度和宽度)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8486414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check image dimensions (height and width) before uploading image using PHP
提问by Hakan
How can I check for height and width before uploading image, using PHP.
如何在上传图像之前使用 PHP 检查高度和宽度。
Must I upload the image first and use "getimagesize()"? Or can I check this before uploading it using 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';
}
}
?>
采纳答案by VolkerK
You need something that is executed on the client before the actual upload happens.
With (server-side) php you can check the dimension only after the file has been uploaded or with upload hooksmaybewhile the image is uploaded (from the image file header data).
您需要在实际上传发生之前在客户端上执行某些操作。
随着(服务器端),PHP,您可以检查该文件已被上传后,才尺寸或上传挂钩可能在图像上载(从图像文件头数据)。
So your options are flash, maybe html5 with its FileAPI (haven't tested that, maybe that's not doable), java-applet, silverlight, ...
所以你的选择是 flash,也许是带有 FileAPI 的 html5(还没有测试过,也许这不可行),java-applet,silverlight,......
回答by Tharanga
We can do this with temp file easily.
我们可以很容易地用临时文件做到这一点。
$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
回答by Hakan
This is how I solved it.
我就是这样解决的。
$test = getimagesize('../bilder/' . $filnamn);
$width = $test[0];
$height = $test[1];
if ($width > 1000 || $height > 1000)
{
echo '<p>iamge is to big';
unlink('../bilder/'.$filnamn);
}
回答by liquorvicar
If the file is in the $_FILES
array (because it's been selected in a Multipart form), it has already been uploaded to the server (usually to /tmp or similar file path) so you can just go ahead and use the getimagesize()
function in php to get the dimensions (including all details as array).
如果文件在$_FILES
数组中(因为它是在 Multipart 形式中被选中的),它已经被上传到服务器(通常是 /tmp 或类似的文件路径),所以你可以继续使用getimagesize()
php 中的函数来获取维度(包括作为数组的所有细节)。
回答by jalalBK
This work for me
这对我有用
$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;
}
回答by Channaveer Hakari
To get the width and height of the image use getimagesize(path_name)
, this function returns the array which contains the height, width, image_type constant and other image related info. By the following code you can achive that.
要获取图像的宽度和高度getimagesize(path_name)
,该函数返回包含高度、宽度、image_type 常量和其他图像相关信息的数组。通过以下代码,您可以实现这一目标。
Note-
Need to pass temporary location of the image, and use the following piece of code before you use move_upload_file()
, else it will move the file to destination path and you wont get the desired result for the image
注意- 需要传递图像的临时位置,并在使用之前使用以下代码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
}
回答by Rizvan
array getimagesize ( string $filename [, array &$imageinfo ] )
The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.
getimagesize() 函数将确定任何给定图像文件的大小,并返回尺寸以及文件类型和要在普通 HTML IMG 标签内使用的高度/宽度文本字符串和相应的 HTTP 内容类型。
For more update visit site: http://www.php.net/manual/en/function.getimagesize.php
更多更新请访问网站:http: //www.php.net/manual/en/function.getimagesize.php
回答by user2976773
Very important - if you are using Dreamweaver to code and you try to get the image size using the $_FILES[anyFormName][tmp_name]
it will display an error in live view.. It took me a while to figure this out.
非常重要 - 如果您使用 Dreamweaver 进行编码并且您尝试使用$_FILES[anyFormName][tmp_name]
它来获取图像大小,它将在实时视图中显示错误。我花了一段时间才弄清楚这一点。