From a406cdec66928fb0d08f829651b66cb10f6a28e0 Mon Sep 17 00:00:00 2001 From: moodler Date: Wed, 20 Nov 2002 14:52:32 +0000 Subject: [PATCH] Made the user profile a little more robust. 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. --- user/edit.html | 10 ++++--- user/edit.php | 71 +++++++------------------------------------------- user/lib.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 64 deletions(-) diff --git a/user/edit.html b/user/edit.html index 7931c62a8a..45c1a41256 100644 --- a/user/edit.html +++ b/user/edit.html @@ -19,10 +19,11 @@ if (isadmin()) { if (isset($err["newpassword"])) { formerr($err["newpassword"]); } else if (empty($user->newpassword)) { - echo "(".get_string("leavetokeep").")"; + echo " (".get_string("leavetokeep").")"; } echo ""; echo ""; + echo "
"; } ?> @@ -124,9 +125,12 @@ if (isadmin()) {

: - + "; + } ?> + - diff --git a/user/edit.php b/user/edit.php index c0cf126aa1..c303e34be0 100644 --- a/user/edit.php +++ b/user/edit.php @@ -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; } @@ -196,6 +140,11 @@ print_heading( get_string("userprofilefor", "", "$userfullname") ); print_simple_box_start("center", "", "$THEME->cellheading"); + if ($err) { + echo "

"; + notify(get_string("someerrorswerefound")); + echo "
"; + } include("edit.html"); print_simple_box_end(); print_footer($course); diff --git a/user/lib.php b/user/lib.php index f8e0279047..53623fc122 100644 --- a/user/lib.php +++ b/user/lib.php @@ -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; -- 2.39.5