Forum Moderators: coopster
Thanks.
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
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)
[edit]
Try something like this:
list($width, $height) = getimagesize($_FILES['userphoto']['tmp_name']);
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]
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.
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
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