Forum Moderators: coopster

Message Too Old, No Replies

Image Upload - Dimension Restrictions

Just need somthing simple

         

Jeigh

4:50 am on Jun 16, 2007 (gmt 0)

10+ Year Member



I have an image uploading system in place on my site but I need to put a size restriction on the uploads (I have a file size restriction but not dimensions now). I've been searching and searching but all I can find is creating thumbnails and cropping with GD, I just want somthing simple though, if the dimensions are over say 500x500 they will get an error.

Thanks.

dreamcatcher

5:31 am on Jun 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jeigh,

You can use getimagesize [php.net] to get the current sizes. So, use that to check the file after it has been uploaded and resize to suit. You should find some resize classes on the PHP Classes Repository [phpclasses.org].

Good luck,

dc

Jeigh

5:39 am on Jun 16, 2007 (gmt 0)

10+ Year Member



Thanks, I'll take a look now :)

Jeigh

6:15 am on Jun 16, 2007 (gmt 0)

10+ Year Member



Well, as I've said before I know very little about making a PHP script work, but I threw a little somthing together of what I thought might work (it didn't of course :P) But am I on the right track?

list($width, $height) = getimagesize("$_FILES['userphoto']");
$maxheight = 400;
$maxwidth = 400;

(Other conditions)

if ($width > $maxwidth) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

if ($height > $maxheight) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

(Other Conditions)

eelixduppy

1:19 pm on Jun 16, 2007 (gmt 0)



Are you getting any errors?

[edit]
Try something like this:


list($width, $height) = getimagesize($_FILES['userphoto']['tmp_name']);

Jeigh

2:49 pm on Jun 16, 2007 (gmt 0)

10+ Year Member



Cheers, I'll try it now and let you know.

Jeigh

2:53 pm on Jun 16, 2007 (gmt 0)

10+ Year Member



I tried that, it comes up with this error though:

Parse error: parse error, unexpected $ in /path/to/file/changephotoparse.php on line 59

Which seems odd.

[edited by: Jeigh at 2:54 pm (utc) on June 16, 2007]

eelixduppy

3:00 pm on Jun 16, 2007 (gmt 0)



What is on line 59?

Jeigh

3:12 pm on Jun 16, 2007 (gmt 0)

10+ Year Member



59: include('advert.php');

Which is why I found it odd.

Entire script:

<? include('header.php');
include('navigation.php');
include('content.php');

$maxfilesize = 204800;
list($width, $height) = getimagesize($_FILES['userphoto']['tmp_name']);
$maxwidth = 150;
$maxheight = 150;
// check if there was a file uploaded
if (!is_uploaded_file($_FILES['userphoto']['tmp_name'])) {
$error = "You didn't select a file to upload.<br />";
// if it was, go ahead with other checks
} else {
if ($_FILES['userphoto']['size'] > $maxfilesize) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

if ($width > $maxwidth) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

if ($height > $maxheight) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

$ext = strrchr($_FILES['userphoto']['name'], ".");
if ($ext!= ".gif" AND $ext!= ".jpg" AND $ext!= ".jpeg" AND $ext!= ".bmp" AND $ext!= ".GIF" AND $ext!= ".JPG" AND $ext!= ".JPEG" AND $ext!= ".BMP") {
$error = "your file was an unacceptable type.<br />";
unlink($_FILES['userphoto']['tmp_name']);

// if it's there, an okay size and type, copy to server and update the photo value in SQL
} else {
if ($_SESSION['photo']!= "nopic.jpg") {
unlink("uploads/photo/".$_SESSION['photo']);
}
$newname = $_SESSION['userid'].$ext;
move_uploaded_file($_FILES['userphoto']['tmp_name'],"uploads/photo/".$newname);
mysql_query("UPDATE users SET photo='$newname' WHERE username='$username'") or die (mysql_error());
$_SESSION['photo'] = $newname;
}
}
}
?>
<h1>Change Photo Result</h1>
<?
if ($error) {
echo "Your photo could not be changed because ".$error.".";
} else {
echo "Your photo was successfully uploaded. To view your updated profile, <a href=\"userinfo.php\">click here</a>.";
}

include('endcontent.php');
include('advert.php');
include('footer.php');?>

[edited by: Jeigh at 3:18 pm (utc) on June 16, 2007]

dreamcatcher

5:58 pm on Jun 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unexpected $ is usually a missing brace. Double check you are terminating each statement with a closing brace.

dc

Jeigh

3:11 am on Jun 17, 2007 (gmt 0)

10+ Year Member



Thanks very much :)

It was missing two braces from the two conditions I added:

if ($width > $maxwidth) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

if ($height > $maxheight) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {

So at the end of the script, I added an extra two and now it works.

Jeigh

3:32 am on Jun 17, 2007 (gmt 0)

10+ Year Member



Just one more little problem, when I try changing the values so height and width are different it lets me upload ones that go over the limit.

Habtom

5:46 am on Jun 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> when I try changing the values so height and width are different it lets me upload ones that go over the limit.

The problem could be one is checked and the other is remaining unchecked, how about putting them together like this:

if ($height > $maxheight ¦¦ $width > $maxwidth) {
$error = "Your image file was too large.<br />";
unlink($_FILES['userphoto']['tmp_name']);
} else {
//the other code
}

Habtom

Jeigh

6:57 am on Jun 17, 2007 (gmt 0)

10+ Year Member



Thanks for the suggestion, but I just figured out why it wasn't working (due to my stupidity :P)

When I copied the code over to use for a different form I forgot to change the:

list($width, $height) = getimagesize($_FILES['userphoto']['tmp_name']);

to

list($width, $height) = getimagesize($_FILES['user_example1']['tmp_name']);

It all works not though, Thanks :D

dreamcatcher

12:48 pm on Jun 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working. :)

dc