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

3.2k views Asked by At

How can I get the image resolution in pixel before upload? Any solution to make as I want? Without change big change of this code? Please! Code only limit file size. But I need to limit both of file size and resolution in pixels!

sorry! my english knowledge too bad. I hope you can understand this. Thank you!

 <?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

There are 4 answers

0
Victor On BEST ANSWER

Use getimagesize function:

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

Now you have access to the $width and $height of your image.

0
Drown On

You can use the getimagesize function to get an array with the width and height of a file.

Addition to your existing code :

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

//Do your checks

You can't use $_FILES['file']['size']as it contains the file size and not the dimensions.

0
Varuna On

thank you all I got the solution :)

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

thank you all I got the solution :)

0
Gaurav Raj On

You can use blueimp Javascript-Load-Image library. You can use exif data parsing methods to parse image dpi, width, height and other informations from the image. You can load the image locally from file tags or from the image urls as per requirement.

Mostly jpeg images and raw image formats store information in exif data. Other formats like png don't have provisions for exif data and hence dpi of such images are difficult to extract without actually loading them in image editing tools like photoshop.