function reg_with_pic_entity_presave in Register with Picture 7
Implements hook_entity_presave().
File
- ./
reg_with_pic.module, line 66 - Reg with pic module file.
Code
function reg_with_pic_entity_presave($account, $type) {
// Only handle new users that have an uploaded picture.
// user_save() will handle old users.
if ($type != 'user' || !$account->is_new || empty($account->picture) || !is_object($account->picture)) {
return;
}
$picture = $account->picture;
$info = image_get_info($picture->uri);
$picture_directory = file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
// Set the new uid since it is typically not set at this point. user_save()
// will use this uid if set so no problems setting it early here.
if (empty($account->uid)) {
$account->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')
->fetchField());
}
// Prepare the pictures directory.
file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
$destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '-' . REQUEST_TIME . '.' . $info['extension']);
// Move the temporary file into the final location.
if ($picture = file_move($picture, $destination, FILE_EXISTS_RENAME)) {
$picture->status = FILE_STATUS_PERMANENT;
$account->picture = file_save($picture);
file_usage_add($picture, 'user', 'user', $account->uid);
}
// Update user record with picture fid.
if (isset($account->picture->fid)) {
$account->picture = $account->picture->fid;
}
}