如何在像素中获取上传图像分辨率? (上传前)

时间:2014-11-12 15:58:59

标签: javascript php html ajax image-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/>';
        }
    }
}
?>

4 个答案:

答案 0 :(得分:2)

使用getimagesize function

  

list($ width,$ height,$ type,$ attr)= getimagesize($ _ FILES ['file']);

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

答案 1 :(得分:1)

您可以使用getimagesize函数获取包含文件宽度和高度的数组。

添加现有代码:

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

//Do your checks

您无法使用$_FILES['file']['size'],因为它包含文件大小而非尺寸。

答案 2 :(得分:1)

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

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/>';
    }

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

答案 3 :(得分:0)

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

大多数jpeg图像和原始图像格式将信息存储在exif数据中。像png这样的其他格式没有exif数据的规定,因此很难提取这些图像的dpi而不实际将它们加载到像photoshop这样的图像编辑工具中。