I have added an image field to user accounts using the "Manage Fields" user interface.
I am trying to make a form that users can use to change their picture.
Below is some code I have tried. In it, I explicitly set the URI of the image when I update the user account. However, for some reason, the URI of the account changes itself to "temporary://image_file_name". No image appears on the account page.
function profile_pic_edit_form($form, &$form_state) {
$form['picture_upload'] = array(
'#type' => 'file',
'#title' => t('Upload picture'),
'#size' => 48,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' =>t('Change my picture'),
);
return $form;
}
function profile_pic_edit_form_submit($form, &$form_state) {
$validators = array(
'file_validate_is_image' => array(),
);
if ($file = file_save_upload('picture_upload', $validators)) {
$file->status = FILE_STATUS_PERMANENT;
file_save_upload('picture_upload', $validators);
$info = image_get_info($file->uri);
$destination = 'public://pictures/profile_pics/'.$file->filename;
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
$pic_array = array(
'fid' => $file->fid,
'alt' => '',
'title' => '',
'width' => $info['width'],
'height' => $info['height'],
'uid' => $file->uid,
'filename' => $file->filename,
'uri' => $destination,
'filemime' => $file->filemime,
'filesize' => $file->filesize,
'status' => '1',
'timestamp' => $file->timestamp,
);
}
else {
form_set_error('picture_upload', t("Failed to upload the picture image."));
}
}
$account = user_load(arg(1));
$edit = array(
'field_profile_picture' => array(
'und' => array(
'0' => $form_state['values']['picture'],
),
),
);
user_save($account, $edit);
//$account->field_profile_picture['und'][0]['uri'] is now temporary://image_name.jpg
}