Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use getimagesize() with $_FILES['']?

Tags:

file

php

upload

I am doing an image upload handler and I would like it to detect the dimensions of the image that's been uploaded by the user.

So I start with:

if (isset($_FILES['image'])) etc....

and I have

list($width, $height) = getimagesize(...);

How am i supposed to use them together?

Thanks a lot

like image 634
eric01 Avatar asked Mar 21 '12 06:03

eric01


People also ask

How does getimagesize work?

The getimagesize() function will determine the size of any supported 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 correspondent HTTP content type.

How to get image file size in php?

$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");

How to Validate image files php?

PHP Code to Validate and Upload Image File In PHP, we validate the file type, size and dimension before uploading. The uploaded file data like name size, temporary target are in $_FILES[“image_file”] array. PHP move_uploaded_file function is used to upload the file by accessing file data stored in $_FILES superglobal.

How to get image size in kb in php?

php $file_size = filesize($_SERVER['DOCUMENT_ROOT']. "/Advertisers/2564. jpg"); // Get file size in bytes $file_size = $file_size / 1024; // Get file size in KB echo $file_size; // Echo file size ?>


2 Answers

You can do this as such

$filename = $_FILES['image']['tmp_name'];
$size = getimagesize($filename);

// or

list($width, $height) = getimagesize($filename);
// USAGE:  echo $width; echo $height;

Using the condition combined, here is an example

if (isset($_FILES['image'])) {
    $filename = $_FILES['image']['tmp_name'];
    list($width, $height) = getimagesize($filename);
    echo $width; 
    echo $height;    
}
like image 60
Starx Avatar answered Oct 14 '22 19:10

Starx


Try this for multi images :

for($i=0; $i < count($filenames); $i++){  

    $image_info = getimagesize($images['tmp_name'][$i]);
    $image_width  = $image_info[0];
    $image_height = $image_info[1];
}

Try this for single image :

$image_info = getimagesize($images['tmp_name']);
$image_width  = $image_info[0];
$image_height = $image_info[1];

at least it works for me.

like image 24
Sulung Nugroho Avatar answered Oct 14 '22 19:10

Sulung Nugroho