如何获得以像素为单位的上传图像分辨率?(上传前)


How to get upload image resolution in pixel? (before upload)

如何在上传之前获得以像素为单位的图像分辨率?有什么解决方案可以根据我的意愿制定吗?如果不更改,这个代码会有大的更改吗?请仅限代码限制文件大小。但我需要限制文件大小和像素分辨率

对不起!我的英语知识太差了。我希望你能理解这一点。非常感谢。

 <?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 
    $target_path = "uploads/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
        $validextensions = array("jpeg", "jpg", "png", "gif");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       
      if (($_FILES["file"]["size"][$i] < 500000) //Approx. 500kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>

使用getimagesize函数:

list($width,$height,$type,$attr)=getimage大小($_FILES['file']);

现在您可以访问图像的$width$height

您可以使用getimagesize函数来获取具有文件宽度和高度的数组。

添加到您现有的代码:

$imageSize = getimagesize($_FILES['file']);
$imageWidth = $imageSize[0];
$imageHeigth = $imageSize[1];
//Do your checks

不能使用$_FILES['file']['size'],因为它包含文件大小而不是尺寸。

谢谢大家,我得到了解决方案:)

list($width, $height, $type, $attr) = getimagesize($_FILES["file"]['tmp_name'][$i]);
if($width>200 && $height>300){
    echo $j. ').<span id="error">***Invalid file Size***</span><br/><br/>';
    }

谢谢大家,我得到了解决方案:)

您可以使用blueimp Javascript加载图像库。您可以使用exif数据解析方法从图像中解析图像dpi、宽度、高度和其他信息。您可以根据需要从文件标签或图像URL本地加载图像。

大部分jpeg图像和原始图像格式将信息存储在exif数据中。png等其他格式没有exif数据,因此如果不将这些图像实际加载到photoshop等图像编辑工具中,则很难提取这些图像的dpi。