Check image dimensions (height and width) before uploading image using PHP
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, ...
Get width and height of an image before uploading
For multiple file uploads the following is from the manual - just in case you haven't found it. I hope that this provides sufficient guidance when trying to access the variables after upload.
-----------------------------------
Example #1 Uploading multiple files
-----------------------------------
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
When the above form is submitted, the arrays $_FILES['userfile']
, $_FILES['userfile']['name']
, and $_FILES['userfile']['size']
will be initialized.Each of these will be a numerically indexed array of the appropriate values for the submitted files.For instance, assume that the filenames /home/test/review.html
and /home/test/xwp.out
are submitted. In this case, $_FILES['userfile']['name'][0]
would contain the value review.html
, and $_FILES['userfile']['name'][1]
would contain the value xwp.out
.
Similarly, $_FILES['userfile']['size'][0]
would contain review.html
's file size, and so forth.
PHP Image Upload Checking Dimensions
mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true);
# upload dir settup
$uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/';
$file=$uploaddir . basename($_FILES['file']['name']);
# upload the file first
if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
# ok so check the height and width
# if it is not the width and height assigned delete image and folder
$size = getimagesize($files);
$maxWidth = 500;
$maxHeight = 500;
if ($size[0] > $maxWidth || $size[1] > $maxHeight)
{
unlink($file);
rmdir("./uploads/temp/".$user."/".$mx."/".$hash."/");
rmdir("./uploads/temp/".$user."/".$mx."/");
rmdir("./uploads/temp/".$user."/");
}
else
$result=1;
end if
} else {
# error uploading
$result=$errormsg;
}
Get image height and width PHP
Should be
list($width, $height, $type, $attr) = getimagesize($_FILES["Artwork"]['tmp_name']);
See http://www.php.net/manual/en/features.file-upload.post-method.php Check image dimensions before upload
well, you can also resize the image after upload.
function createFixSizeImage( $pathToImages, $pathToFixSizeImages, $Width )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
$image_info = getimagesize( "path/to/images/".$fname );
$image_width = $image_info[0];
$image_height = $image_info[1];
$image_type = $image_info[2];
switch ( $image_type )
{
case IMAGETYPE_JPEG:
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpeg' )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// give the size,u want
$new_width = 100;
$new_height = 100;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save Fix Size Images into a file
imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );
}
break;
case IMAGETYPE_PNG:
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'png' )
{
// load image and get image size
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100;
$new_height = 100;
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save Fix Size Images into a file
imagejpeg( $tmp_img, "{$pathToFixSizeImages}{$fname}" );
}
break;
case IMAGETYPE_BMP:
echo "bmp";
break;
default:
break;
}
}
}
// close the directory
closedir( $dir );
}
createFixSizeImage("path","path/to/images/to/be/saved",100);
Check image width and height before upload with Javascript
The file is just a file, you need to create an image like so:
var _URL = window.URL || window.webkitURL;
$("#file").change(function (e) {
var file, img;
if ((file = this.files[0])) {
img = new Image();
var objectUrl = _URL.createObjectURL(file);
img.onload = function () {
alert(this.width + " " + this.height);
_URL.revokeObjectURL(objectUrl);
};
img.src = objectUrl;
}
});
Demo: http://jsfiddle.net/4N6D9/1/I take it you realize this is only supported in a few browsers. Mostly firefox and chrome, could be opera as well by now.
P.S. The URL.createObjectURL()
method has been removed from the MediaStream
interface. This method has been deprecated in 2013 and superseded by assigning streams to HTMLMediaElement.srcObject
. The old method was removed because it is less safe, requiring a call to URL.revokeOjbectURL()
to end the stream. Other user agents have either deprecated (Firefox) or removed (Safari) this feature feature.
For more information, please refer here.
Related Topics
PHP Sum Variable in While Loop
How to Install Gmp for PHP7 on Ubuntu
No Hint Path Defined for [Mail] Laravel 5.4
PHP - Good Cronjob/Crontab/Cron Tutorial or Book
Force Logout Users If Users Are Inactive for a Certain Period of Time
Pass Array to Where in Codeigniter Active Record
Built in Support for Sets in PHP
SQL Select * from Multiple Tables
Check If a String Is All Caps in PHP
Why a Full Stop, "." and Not a Plus Symbol, "+", for String Concatenation in PHP
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Check If Two PHP Datetime Objects Are Set to the Same Date ( Ignoring Time )
Can a PHP File Name (Or a Dir in Its Full Path) Have Utf-8 Characters
Different Timezone_Types on Datetime Object
Echo 'String' While Every Long Loop Iteration (Flush() Not Working)
How to Convert Emoji from Unicode in PHP