Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'; }   }        ?> 
like image 417
Hakan Avatar asked Dec 13 '11 08:12

Hakan


People also ask

How can I get image width and height in PHP?

The getimagesize() function in PHP is an inbuilt function which is used to get the size of an image. This function accepts the filename as a parameter and determines the image size and returns the dimensions with the file type and height/width of image.

How can I limit my photo upload size in PHP?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads . Note that from PHP 5.3.

What are the functions to be used to get the image properties size width and height?

The imagesx() and imagesy() are used to extract the width and height of the images respectively.

How to validate the size of an uploaded image in PHP?

I used PHP function getimagesize () to get the size information to validate the uploaded image in this regard. I specified the allowed image file extensions in an array and validate the uploaded file extension with this array. You can change this array with other preferable image file extension as your wish.

How to get the dimensions of a file in PHP?

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

How do I upload an image file in 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 check the dimension of a file before upload?

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


2 Answers

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]; 
like image 189
Tharanga Avatar answered Sep 22 '22 09:09

Tharanga


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); } 
like image 34
Hakan Avatar answered Sep 23 '22 09:09

Hakan