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

Hakan picture Hakan · Dec 13, 2011 · Viewed 90.6k times · Source

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


}







?>

Answer

Tharanga picture Tharanga · Nov 15, 2012

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