Skip to content

Commit

Permalink
Made the user profile a little more robust.
Browse files Browse the repository at this point in the history
Uploaded images are now saved even if the rest of the form
has errors.

If errors are found in the form then a message is printed up
the top to make it clearer that they need to fix something.
  • Loading branch information
moodler committed Nov 20, 2002
1 parent bd4707b commit a406cde
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 64 deletions.
10 changes: 7 additions & 3 deletions user/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
if (isset($err["newpassword"])) {
formerr($err["newpassword"]);
} else if (empty($user->newpassword)) {
echo "(".get_string("leavetokeep").")";
echo " (".get_string("leavetokeep").")";
}
echo "</td>";
echo "</tr>";
echo "<tr><td colspan=2><HR></td></tr>";
}
?>
<tr valign=top>
Expand Down Expand Up @@ -124,9 +125,12 @@
</tr>
<tr valign=top>
<td><P><? print_string("userdescription") ?>:</td>
<td><TEXTAREA NAME=description COLS=50 ROWS=10 WRAP=virtual><? p($user->description) ?></TEXTAREA>
<td><? if ($err["description"]) {
formerr($err["description"]);
echo "<BR>";
} ?>
<TEXTAREA NAME=description COLS=50 ROWS=10 WRAP=virtual><? p($user->description) ?></TEXTAREA>
<? helpbutton("text", get_string("helptext")) ?>
<? formerr($err["description"]) ?>
</td>
</tr>
<tr>
Expand Down
71 changes: 10 additions & 61 deletions user/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,73 +50,17 @@
$usernew->lastname = strip_tags($usernew->lastname);

if (find_form_errors($user, $usernew, $err) ) {
if ($filename = valid_uploaded_file($imagefile)) {
$usernew->picture = save_user_image($user->id, $filename);
}

$user = $usernew;

} else {
$timenow = time();

if ($filename = valid_uploaded_file($imagefile)) {
$imageinfo = GetImageSize($filename);
$image->width = $imageinfo[0];
$image->height = $imageinfo[1];
$image->type = $imageinfo[2];

switch ($image->type) {
case 2: $im = ImageCreateFromJPEG($filename); break;
case 3: $im = ImageCreateFromPNG($filename); break;
default: error("Image must be in JPG or PNG format");
}
if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
$im1 = ImageCreateTrueColor(100,100);
$im2 = ImageCreateTrueColor(35,35);
} else {
$im1 = ImageCreate(100,100);
$im2 = ImageCreate(35,35);
}

$cx = $image->width / 2;
$cy = $image->height / 2;

if ($image->width < $image->height) {
$half = floor($image->width / 2.0);
} else {
$half = floor($image->height / 2.0);
}

if (!file_exists("$CFG->dataroot/users")) {
if (! mkdir("$CFG->dataroot/users", 0777)) {
$badpermissions = true;
}
}
if (!file_exists("$CFG->dataroot/users/$user->id")) {
if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
$badpermissions = true;
}
}

if ($badpermissions) {
$usernew->picture = "0";

} else {
ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);

// Draw borders over the top.
$black1 = ImageColorAllocate ($im1, 0, 0, 0);
$black2 = ImageColorAllocate ($im2, 0, 0, 0);
ImageLine ($im1, 0, 0, 0, 99, $black1);
ImageLine ($im1, 0, 99, 99, 99, $black1);
ImageLine ($im1, 99, 99, 99, 0, $black1);
ImageLine ($im1, 99, 0, 0, 0, $black1);
ImageLine ($im2, 0, 0, 0, 34, $black2);
ImageLine ($im2, 0, 34, 34, 34, $black2);
ImageLine ($im2, 34, 34, 34, 0, $black2);
ImageLine ($im2, 34, 0, 0, 0, $black2);

ImageJpeg($im1, "$CFG->dataroot/users/$user->id/f1.jpg", 90);
ImageJpeg($im2, "$CFG->dataroot/users/$user->id/f2.jpg", 95);
$usernew->picture = "1";
}
$usernew->picture = save_user_image($user->id, $filename);
} else {
$usernew->picture = $user->picture;
}
Expand Down Expand Up @@ -196,6 +140,11 @@

print_heading( get_string("userprofilefor", "", "$userfullname") );
print_simple_box_start("center", "", "$THEME->cellheading");
if ($err) {
echo "<CENTER>";
notify(get_string("someerrorswerefound"));
echo "</CENTER>";
}
include("edit.html");
print_simple_box_end();
print_footer($course);
Expand Down
71 changes: 71 additions & 0 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,77 @@ function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $
}
}


function save_user_image($userid, $filename) {
// Given a filename to a known image, this function scales and crops
// it and saves it in the right place to be a user image.

global $CFG;

$imageinfo = GetImageSize($filename);
$image->width = $imageinfo[0];
$image->height = $imageinfo[1];
$image->type = $imageinfo[2];

switch ($image->type) {
case 2: $im = ImageCreateFromJPEG($filename); break;
case 3: $im = ImageCreateFromPNG($filename); break;
default: return 0;
}
if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
$im1 = ImageCreateTrueColor(100,100);
$im2 = ImageCreateTrueColor(35,35);
} else {
$im1 = ImageCreate(100,100);
$im2 = ImageCreate(35,35);
}

$cx = $image->width / 2;
$cy = $image->height / 2;

if ($image->width < $image->height) {
$half = floor($image->width / 2.0);
} else {
$half = floor($image->height / 2.0);
}

if (!file_exists("$CFG->dataroot/users")) {
if (! mkdir("$CFG->dataroot/users", 0777)) {
$badpermissions = true;
}
}
if (!file_exists("$CFG->dataroot/users/$user->id")) {
if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
$badpermissions = true;
}
}

if ($badpermissions) {
return 0;

} else {
ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);

// Draw borders over the top.
$black1 = ImageColorAllocate ($im1, 0, 0, 0);
$black2 = ImageColorAllocate ($im2, 0, 0, 0);
ImageLine ($im1, 0, 0, 0, 99, $black1);
ImageLine ($im1, 0, 99, 99, 99, $black1);
ImageLine ($im1, 99, 99, 99, 0, $black1);
ImageLine ($im1, 99, 0, 0, 0, $black1);
ImageLine ($im2, 0, 0, 0, 34, $black2);
ImageLine ($im2, 0, 34, 34, 34, $black2);
ImageLine ($im2, 34, 34, 34, 0, $black2);
ImageLine ($im2, 34, 0, 0, 0, $black2);

ImageJpeg($im1, "$CFG->dataroot/users/$userid/f1.jpg", 90);
ImageJpeg($im2, "$CFG->dataroot/users/$userid/f2.jpg", 95);
return 1;
}
}


function print_user($user, $course, $string) {

global $USER, $COUNTRIES;
Expand Down

0 comments on commit a406cde

Please sign in to comment.