...

View Full Version : Resolved Limiting a file upload by dimensions (wxh)



greens85
07-06-2010, 02:36 PM
Hi all,

I have an image upload facility at present, but due to the design of my site I can only allow users to upload images of certain dimensions.. I've seen a lot of stuff on file size restrictions but never come across anything that limits by dimensions.

I have in the past done resizing, but I've always found the quality turns out rubbish! Since I want to limit the image uploads to be 468(w)x60(h)... if the user uploaded lets say a square image then the end result would be terrible.

Is it possible to actually limit files by their dimensions?

Many thanks,

Greens85

Fumigator
07-06-2010, 03:52 PM
You can, but only after the file has been uploaded to your server. Before that you have no way of knowing anything about the file. After upload you can check the dimensions using getimagesize().

http://us3.php.net/manual/en/function.getimagesize.php

Keleth
07-06-2010, 03:54 PM
You can use the PHP function getimagesize to check the size of the image during validation... if the size isn't what you want, return an error instead of proceeding.

http://php.net/manual/en/function.getimagesize.php

greens85
07-07-2010, 09:43 AM
Hi guys,

Thanks for the responses, I always have a hard time following the php manual, perhaps because i'm a newbie im not sure...

I have tried my best to understand it and added the following to my existing code:


$ran = time();
$imgExtArr = explode('.', $_FILES['bannerimg']['name']);
$imgExt = $imgExtArr[1];
$imageName = $ran.'.'.$imgExt;
$uploadpath = 'images/ads/banners/'.$imageName;
move_uploaded_file($_FILES['bannerimg']['tmp_name'],$uploadpath);
$size = getimagesize('$uploadpath');
echo $size;
exit();

However when I run the script I just get an error of:

Warning: getimagesize($uploadpath) [function.getimagesize]: failed to open stream: No such file or directory in /path/to/file on line 55

abduraooft
07-07-2010, 10:14 AM
$size = getimagesize('$uploadpath');
You are making silly mistakes. PHP doesn't parse the php variable inside single quotes. Remove the quotes and you'll get a different result.

PS: I'd recommend you to check the php manual pages of getimagesize() to see the return values and usage before posting another "doesn't work" reply :)

greens85
07-07-2010, 10:29 AM
You are making silly mistakes. PHP doesn't parse the php variable inside single quotes. Remove the quotes and you'll get a different result.

PS: I'd recommend you to check the php manual pages of getimagesize() to see the return values and usage before posting another "doesn't work" reply :)

Hi Abduraooft,

I have made a fair amount of progress now, so hopefully it is fine for me to post :)

This is what I have now:


$root = $_SERVER['DOCUMENT_ROOT'];
$completePath = $root.'/'.$uploadpath;
$ImageDetails = getimagesize($completePath);
$width = $ImageDetails[3];
echo $width;

And this displays:

width="468" height="60"

Which is obviously exactly what I am after, however I was expecting the width and height to be returned as different parts of the arrays...

at which point I was going to add an if statement like so:


if ($width !=486 && $height !=60) {
// failed
} else {
// go ahead
}

But now they have been returned as one, I'm slightly confused.

greens85
07-07-2010, 10:43 AM
Not to worry I found a way to do it:

For anyone who is interested or having the same problems...


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

echo $width;
'<br/>';
echo $height;

That will separate the width and height :)

Fumigator
07-07-2010, 03:42 PM
In the manual it tells you getimagesize() returns width in the 0 index and height in the 1 index. So you could just do:



$ImageDetails = getimagesize($completePath);
$width = $ImageDetails[0];
$width = $ImageDetails[1];


But your solution kind of does the same thing. Just so you understand, they are returned separately for you.

Keleth
07-07-2010, 04:11 PM
Sorry Fumigator, just have to do this... copy/paste fail! :p



$ImageDetails = getimagesize($completePath);
$width = $ImageDetails[0];
$height = $ImageDetails[1];

Fumigator
07-07-2010, 06:27 PM
Whoops :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum