使用 PHP 上传图像之前检查图像尺寸(高度和宽度)

发布于 2024-12-21 01:37:01 字数 674 浏览 6 评论 0原文

如何使用 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';
}


}







?>

How can I check for height and width before uploading image, using PHP.

Must I upload the image first and use "getimagesize()"? Or can I check this before uploading it using 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';
}


}







?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦在深巷 2024-12-28 01:37:01

我们可以使用临时文件轻松地做到这一点。

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];

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];
坏尐絯 2024-12-28 01:37:01

我就是这样解决的。

$test = getimagesize('../bilder/' . $filnamn);
$width = $test[0];
$height = $test[1];

if ($width > 1000 || $height > 1000)
{
echo '<p>iamge is to big';
unlink('../bilder/'.$filnamn);
}

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);
}
诺曦 2024-12-28 01:37:01

这对我来说工作

$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;
}

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;
}
沫离伤花 2024-12-28 01:37:01

如果文件位于 $_FILES 数组中(因为它是在 Multipart 表单中选择的),则它已经上传到服务器(通常上传到 /tmp 或类似的文件路径),因此您可以继续并使用 php 中的 getimagesize() 函数来获取尺寸(包括作为数组的所有详细信息)。

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).

源来凯始玺欢你 2024-12-28 01:37:01

要获取图像的宽度和高度,请使用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
}

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.

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

$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
}
ゝ偶尔ゞ 2024-12-28 01:37:01

您需要在实际上传发生之前在客户端上执行一些操作。
使用(服务器端)php,您只能在文件上传后或使用 上传挂钩也许在上传图像时(从图像文件头数据)。

所以你的选择是 flash,也许是 html5 及其 FileAPI(尚未测试过,也许这是不可行的),java-applet,silverlight,...

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 hooks maybe while the image is uploaded (from the image file header data).

So your options are flash, maybe html5 with its FileAPI (haven't tested that, maybe that's not doable), java-applet, silverlight, ...

南风几经秋 2024-12-28 01:37:01
 array getimagesize ( string $filename [, array &$imageinfo ] )

getimagesize() 函数将确定任何给定图像文件的大小,并返回尺寸以及文件类型以及要在普通 HTML IMG 标记和相应的 HTTP 内容类型中使用的高度/宽度文本字符串。

更多更新请访问网站:
http://www.php.net/manual/en/function.getimagesize.php

 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.

For more update visit site:
http://www.php.net/manual/en/function.getimagesize.php

你在我安 2024-12-28 01:37:01

非常重要 - 如果您使用 Dreamweaver 进行编码,并且尝试使用 $_FILES[anyFormName][tmp_name] 获取图像大小,它将在实时视图中显示错误。我花了一段时间弄清楚这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文