使用 PHP 上传图像之前检查图像尺寸(高度和宽度)
如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我们可以使用临时文件轻松地做到这一点。
We can do this with temp file easily.
我就是这样解决的。
This is how I solved it.
这对我来说工作
This work for me
如果文件位于
$_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 thegetimagesize()
function in php to get the dimensions (including all details as array).要获取图像的宽度和高度,请使用
getimagesize(path_name)
,该函数返回包含高度、宽度、image_type 常量和其他图像相关信息的数组。通过下面的代码你可以实现这一点。注意 -
需要传递图像的临时位置,并在使用
move_upload_file()
之前使用以下代码,否则它将把文件移动到目标路径,并且您将无法获得图像所需的结果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您需要在实际上传发生之前在客户端上执行一些操作。
使用(服务器端)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, ...
getimagesize() 函数将确定任何给定图像文件的大小,并返回尺寸以及文件类型以及要在普通 HTML IMG 标记和相应的 HTTP 内容类型中使用的高度/宽度文本字符串。
更多更新请访问网站:
http://www.php.net/manual/en/function.getimagesize.php
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
非常重要 - 如果您使用 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.