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

8

32

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


}







?>
Longship answered 13/12, 2011 at 8:49 Comment(2)
You cannot check a file using PHP that is not uploaded to the server. If it has to be PHP, it's not possible.Haigh
Guess I have to upload it then, and "unlik" it if it exceed 1000px :) Or use JavaScript if I can make it an option.Longship
T
4

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

Trish answered 13/12, 2011 at 8:54 Comment(0)
G
111

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];
Guipure answered 15/11, 2012 at 8:29 Comment(1)
Works perfect! I have tested it myself, and the image size is displayed perfectly.Thacker
L
10

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);
}
Longship answered 13/12, 2011 at 9:4 Comment(1)
Why check after image is already uploaded? & then spending time to unlink againCountersubject
M
6

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

Microbicide answered 13/12, 2011 at 8:53 Comment(0)
E
6

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;
}
Elise answered 9/10, 2015 at 11:34 Comment(1)
As a good practice you should also explain your answer a bit as it helps the understandingRossner
S
5

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
}
Suborbital answered 6/2, 2014 at 7:25 Comment(0)
T
4

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

Trish answered 13/12, 2011 at 8:54 Comment(0)
L
0
 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

Lenis answered 13/12, 2011 at 9:1 Comment(0)
I
0

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.

Introductory answered 20/2, 2014 at 1:48 Comment(1)
did you set your form as "multipart/form-data"?Cambogia

© 2022 - 2024 — McMap. All rights reserved.