reg_with_pic.module in Register with Picture 5
Same filename and directory in other branches
This module allows a user picture to be uploaded at registration time.
File
reg_with_pic.moduleView source
<?php
/**
* @file This module allows a user picture to be uploaded at registration time.
*/
/**
* Implementation of hook_user()
*/
function reg_with_pic_user($op, &$edit, &$user, $category = null) {
if (variable_get('user_pictures', 0)) {
switch ($op) {
case 'register':
// setup picture upload in registration form
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['picture'] = array(
'#type' => 'fieldset',
'#title' => t('Picture'),
'#weight' => 1,
);
$form['picture']['picture_upload_register'] = array(
'#type' => 'file',
'#title' => t('Upload picture'),
'#description' => t('<br>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', ''),
);
return $form;
break;
case 'validate':
// validate uploaded picture
if ($file = file_check_upload('picture_upload_register')) {
reg_with_pic_validate_picture($file, $edit, $user);
}
break;
case 'insert':
// copy picture to the proper directory now that we have the userid.
$file = file_check_upload('picture_upload_register');
$info = image_get_info($file->filepath);
// upload file and write path to user record
if ($file = file_save_upload('picture_upload_register', variable_get('user_picture_path', 'pictures') . '/picture-' . $user->uid . '.' . $info['extension'], 1)) {
db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
}
break;
}
}
}
/**
* Picture validation taken from user module. We re-wrote the function here because
* the original function copied in here. The problem is that we didn't have the
* userid at the time of registration so we took that out and write the picture
* in the insert case of the user hook.
*/
function reg_with_pic_validate_picture($file, &$edit, $user) {
global $form_values;
// Initialize the picture:
$form_values['picture'] = $user->picture;
// Check that uploaded file is an image, with a maximum file size
// and maximum height/width.
$info = image_get_info($file->filepath);
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if (!$info || !$info['extension']) {
form_set_error('picture_upload_register', t('The uploaded file was not an image.'));
}
else {
if (image_get_toolkit()) {
image_scale($file->filepath, $file->filepath, $maxwidth, $maxheight);
}
else {
if (filesize($file->filepath) > variable_get('user_picture_file_size', '30') * 1000) {
form_set_error('picture_upload_register', t('The uploaded image is too large; the maximum file size is %size kB.', array(
'%size' => variable_get('user_picture_file_size', '30'),
)));
}
else {
if ($info['width'] > $maxwidth || $info['height'] > $maxheight) {
form_set_error('picture_upload_register', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array(
'%dimensions' => variable_get('user_picture_dimensions', '85x85'),
)));
}
}
}
}
}
Functions
Name![]() |
Description |
---|---|
reg_with_pic_user | Implementation of hook_user() |
reg_with_pic_validate_picture | Picture validation taken from user module. We re-wrote the function here because the original function copied in here. The problem is that we didn't have the userid at the time of registration so we took that out and write the picture in the… |