I programmed a multi-step form using the drupal form API. On the first page is
a user image file upload using the drupal managed field managed_file. When the form is stepped thru to the end and submitted, the already in the single steps validated data will be stored into profile2, by creating a profile2 object with profile_create
and set the assign the fields.
This all works without problem. But how can I store the FID of the uploaded file to the profile2 user_image field? A s I saw in mysql, the file FID and the measurements of the image are stored in the field table.
I have this (file_load code snippet is untested) in the form submit step, where the data gets stored into profile2:
$profile = profile_create(array
'user' => $new_user,
'type' => 'main'));
$profile->field_firstname['und'][0]['value'] = $user_data_collection['t_1_1'];
// ...
if (isset($user_data_collection['t_1_22'])) {
$file = file_load($user_data_collection['t_1_22']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
$fid = $file->fid;
// --- how can I store the FID, x and y measurements of the image?
// ...
}
// ...
profile2_save($profile);
Can somebody give me a hint? I was searching on google but did not find an answer to my question. Thanks in advance!
Update:
This is how I solved it now. Since I work with an "managed_file" Field in the form, and later for displaying the image file via the theme()
fct I do not have to care about resizing. This is done due to the image cache / theme fct's with a properly defined image-style.
$profile = profile_create(array(
'user' => $new_user,
'type' => 'main'));
//...
if (isset($user_data_collection['t_1_22'])) {
$file = file_load($user_data_collection['t_1_22']);
// --- Flag that this file is permanent and should not be deleted.
$file->status = FILE_STATUS_PERMANENT;
// --- save file object to the database
file_save($file);
// --- get original x/y measurements for this image
$img_info = image_get_info(drupal_realpath($file->uri));
// --- store fid. x and y to profile 2
$profile->field_image_user[
'und'][0]['fid'] = $file->fid;
$profile->field_image_user[
'und'][0]['width'] = $img_info['width'];
$profile->field_image_user[
'und'][0]['height'] = $img_info['height'];
}
//...
// --- save profile data
profile2_save($profile);
So all seems to work and have the solution that I needed.