Hakan
Hakan

Reputation: 3885

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

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?

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


}







?>

Upvotes: 32

Views: 102450

Answers (8)

liquorvicar
liquorvicar

Reputation: 6106

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

Upvotes: 6

user2976773
user2976773

Reputation: 59

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.

Upvotes: 0

jalalBK
jalalBK

Reputation: 61

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.";
    exit;
}

Upvotes: 6

Channaveer Hakari
Channaveer Hakari

Reputation: 69

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.

Note - 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']);
print_r($imageInformation);

$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
}

Upvotes: 5

Tharanga
Tharanga

Reputation: 2767

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];

Upvotes: 112

Hakan
Hakan

Reputation: 3885

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';
unlink('../bilder/'.$filnamn);
}

Upvotes: 10

Rizvan
Rizvan

Reputation: 717

 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: http://www.php.net/manual/en/function.getimagesize.php

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

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

Upvotes: 4

Related Questions