Commit 9cb5f7cd authored by Steven Wittens's avatar Steven Wittens

Avatar/picture fixes:

- Changing theme('image') so the automatic image size fetching can be toggled independently from attributes. Specifying attributes and autosizing are 2 different things.
- Suppressing PHP errors from getimagesize() using @. drupal_set_message() is used to report these errors already and in a much prettier way.
- #9958: Fixing broken displaying of avatars.
- Don't show the default avatar in 'edit my account' if the user has no avatar of his/her own.
- Added ability to delete avatars (without having to replace them).
parent 2436854a
......@@ -242,13 +242,14 @@ function theme_links($links, $delimiter = ' | ') {
* @param $title
* The title text is displayed when the image is hovered in some popular browsers.
* @param $attr
* Attributes placed in the img tag.
* This parameter overrides the default auto-detection of width and height.
* Attributes placed in the img tag.
* @param $getsize
* If set to true, the image's dimension are fetched and added as width/height attributes.
* @return
* A string containing the image tag.
*/
function theme_image($path, $alt = NULL, $title = NULL, $attr = NULL) {
if (isset($attr) || (file_exists($path) && (list($width, $height, $type, $attr) = getimagesize($path)))) {
function theme_image($path, $alt = '', $title = '', $attr = '', $getsize = true) {
if (!$getsize || (file_exists($path) && (list($width, $height, $type, $attr) = @getimagesize($path)))) {
return "<img src=\"$path\" $attr alt=\"$alt\" title=\"$title\" />";
}
}
......
......@@ -183,7 +183,7 @@ function user_validate_picture($file, &$edit, $user) {
// Check that uploaded file is an image, with a maximum file size
// and maximum height/width.
$extension = strtolower(strrchr($file->filename, '.'));
$size = getimagesize($file->filepath);
$size = @getimagesize($file->filepath);
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) {
......@@ -196,7 +196,7 @@ function user_validate_picture($file, &$edit, $user) {
form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))));
}
else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR .'picture-'. $user->uid . $extension, 1)) {
$edit['picture'] = $file->path;
$edit['picture'] = $file->filepath;
}
else {
form_set_error('picture', t("Failed to upload the picture image; the %directory directory doesn't exist.", array('%directory' => '<em>'. variable_get('user_picture_path', 'pictures') .'</em>')));
......@@ -352,7 +352,7 @@ function user_perm() {
*/
function user_file_download($file) {
if (strpos($file, variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR . 'picture-') === 0) {
list($width, $height, $type, $attr) = getimagesize(file_create_path($file));
list($width, $height, $type, $attr) = @getimagesize(file_create_path($file));
$types = array(
IMAGETYPE_GIF => 'image/gif',
IMAGETYPE_JPEG => 'image/jpeg',
......@@ -547,7 +547,7 @@ function theme_user_picture($account) {
if ($picture) {
$alt = t('%user\'s picture', array('%user' => $account->name ? $account->name : t(variable_get('anonymous', 'Anonymous'))));
$picture = theme('image', $picture, $alt, $alt);
$picture = theme('image', $picture, $alt, $alt, '', false);
if ($account->uid) {
$picture = l($picture, "user/$account->uid", array('title' => t('View user profile.')));
}
......@@ -998,8 +998,9 @@ function user_edit_form($uid, $edit) {
// Picture/avatar:
if (variable_get('user_pictures', 0)) {
$group = '';
if (file_exists($edit['picture'])) {
$group .= theme('image', file_create_url($edit['picture']));
if ($edit['picture'] && ($picture = theme_user_picture(array2object($edit)))) {
$group .= $picture;
$group .= form_checkbox(t('Delete picture'), 'picture_delete', 1, 0, t('Check this box to delete your current picture.'));
}
$group .= form_file(t('Upload picture'), 'picture', 48, t('Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
$data[] = array('title' => t('Picture'), 'data' => $group, 'weight' => 1);
......@@ -1036,6 +1037,14 @@ function user_edit_validate($uid, &$edit) {
$user = user_load(array('uid' => $uid));
user_validate_picture($file, $edit, $user);
}
// Delete picture if requested, and if no replacement picture was given.
else if ($edit['picture_delete']) {
$user = user_load(array('uid' => $uid));
if ($user->picture && file_exists($user->picture)) {
file_delete($user->picture);
}
$edit['picture'] = '';
}
// If required, check that proposed passwords match. If so, add the new password to $edit.
if ($edit['pass1']) {
......
......@@ -183,7 +183,7 @@ function user_validate_picture($file, &$edit, $user) {
// Check that uploaded file is an image, with a maximum file size
// and maximum height/width.
$extension = strtolower(strrchr($file->filename, '.'));
$size = getimagesize($file->filepath);
$size = @getimagesize($file->filepath);
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) {
......@@ -196,7 +196,7 @@ function user_validate_picture($file, &$edit, $user) {
form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'))));
}
else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR .'picture-'. $user->uid . $extension, 1)) {
$edit['picture'] = $file->path;
$edit['picture'] = $file->filepath;
}
else {
form_set_error('picture', t("Failed to upload the picture image; the %directory directory doesn't exist.", array('%directory' => '<em>'. variable_get('user_picture_path', 'pictures') .'</em>')));
......@@ -352,7 +352,7 @@ function user_perm() {
*/
function user_file_download($file) {
if (strpos($file, variable_get('user_picture_path', 'pictures') . FILE_SEPARATOR . 'picture-') === 0) {
list($width, $height, $type, $attr) = getimagesize(file_create_path($file));
list($width, $height, $type, $attr) = @getimagesize(file_create_path($file));
$types = array(
IMAGETYPE_GIF => 'image/gif',
IMAGETYPE_JPEG => 'image/jpeg',
......@@ -547,7 +547,7 @@ function theme_user_picture($account) {
if ($picture) {
$alt = t('%user\'s picture', array('%user' => $account->name ? $account->name : t(variable_get('anonymous', 'Anonymous'))));
$picture = theme('image', $picture, $alt, $alt);
$picture = theme('image', $picture, $alt, $alt, '', false);
if ($account->uid) {
$picture = l($picture, "user/$account->uid", array('title' => t('View user profile.')));
}
......@@ -998,8 +998,9 @@ function user_edit_form($uid, $edit) {
// Picture/avatar:
if (variable_get('user_pictures', 0)) {
$group = '';
if (file_exists($edit['picture'])) {
$group .= theme('image', file_create_url($edit['picture']));
if ($edit['picture'] && ($picture = theme_user_picture(array2object($edit)))) {
$group .= $picture;
$group .= form_checkbox(t('Delete picture'), 'picture_delete', 1, 0, t('Check this box to delete your current picture.'));
}
$group .= form_file(t('Upload picture'), 'picture', 48, t('Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
$data[] = array('title' => t('Picture'), 'data' => $group, 'weight' => 1);
......@@ -1036,6 +1037,14 @@ function user_edit_validate($uid, &$edit) {
$user = user_load(array('uid' => $uid));
user_validate_picture($file, $edit, $user);
}
// Delete picture if requested, and if no replacement picture was given.
else if ($edit['picture_delete']) {
$user = user_load(array('uid' => $uid));
if ($user->picture && file_exists($user->picture)) {
file_delete($user->picture);
}
$edit['picture'] = '';
}
// If required, check that proposed passwords match. If so, add the new password to $edit.
if ($edit['pass1']) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment