Check image dimensions (height and width) before uploading image using PHP-ThrowExceptions

Exception or error:

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?


foreach ($_FILES["files"]["error"] as $key => $error) {
$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];

echo '<p>image dimenssions must be less than 1000px width and 1000px height';


How to solve:

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, …


We can do this with temp file easily.

$image_info = getimagesize($_FILES["file_field_name"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];


This is how I solved it.

$test = getimagesize('../bilder/' . $filnamn);
$width = $test[0];
$height = $test[1];

if ($width > 1000 || $height > 1000)
echo '<p>iamge is to big';


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 the getimagesize() function in php to get the dimensions (including all details as array).


This work for me

$file = $_FILES["files"]['tmp_name'];
list($width, $height) = getimagesize($file);

if($width > "180" || $height > "70") {
    echo "Error : image size must be 180 x 70 pixels.";


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.

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

$imageInformation = getimagesize($_FILES['celebrity_pic']['tmp_name']);

$imageWidth = $imageInformation[0]; //Contains the Width of the Image

$imageHeight = $imageInformation[1]; //Contains the Height of the Image

if($imageWidth >= your_value && $imageHeight >= your_value)
  //Your Code


 array getimagesize ( string $filename [, array &$imageinfo ] )

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:


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.

Leave a Reply

Your email address will not be published. Required fields are marked *