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

2021-12-24 00:00:00 file-upload image php dimensions

如何在上传图片之前使用 PHP 检查高度和宽度.

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

我必须先上传图像并使用getimagesize()"吗?或者我可以在使用 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';
}


}







?>

推荐答案

您需要在实际上传发生之前在客户端上执行某些操作.
使用(服务器端)php,您只能在文件上传后或使用 upload hooks 可能在上传图像时(来自图像文件头数据).

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

所以你的选择是 flash,也许是带有 FileAPI 的 html5(还没有测试过,也许那不可行),java-applet,silverlight,...

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

相关文章