0

I have dropdown list with 2 options. First one is to upload image, where I use image upload field and imagecashe for re-sizing uploaded image. And Second option is to get picture from other site.

If user choose to get picture from other site, i don't know how to use downloaded image.

I downloaded image using following code

$managed = TRUE; $path = system_retrieve_file($image, 'public://down_images/', $managed);

When this image is downloaded I want to use it as logo. Logo is image field which is added to user account.

How can I do that?

| improve this question | | | | |
  • You want to use it as site logo? or want to assign with any user field? – ARUN Dec 7 '13 at 12:52
1

One way to achieve this is to :

  1. Have custom hook_form_alter implemented which will take the external image URL in custom field.

  2. Create additional cck field in your content type and hide it while displaying in form

  3. After submission you will download and save the file using file_copy functions

  4. Once the image is downloaded you will attach it (programmatically) to the cck field created @step2 in the form_submit.

Hope this helps.

| improve this answer | | | | |
  • I can load file using following query [code] $file = file_load(db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField());[code] then I made a copy [code]$file = file_copy($file, 'public://companylogo');[code] and how can I upload image? This field is on user_profile_form I try with below code but no success [code]$form_state['values']['field_company_logo'] = (array) $file; $user->field_company_logo[$user->language][0] = (array) $file;[code] – miske May 16 '12 at 10:24
1
  1. You need to get the file fid; you can use EntityFieldQuery.
  2. Then insert/update the "file_managed" table, using code similar to the following one:

    db_update('file_usage')
      ->fields(array('id' => $user->uid,))  
      ->condition('fid', $file->fid)  
      ->execute();
    
| improve this answer | | | | |
  • I made your suggested changes but nothing happen. Also I changed your query to the following db_update('file_usage')->fields(array( 'fid' => $file->fid, )) ->condition('id', $user->uid)->execute(); After query is finished, changes are visible in tables file_usage and file_managed, but there is old default(or uploaded) picture on page. – miske May 16 '12 at 10:18
1

If you have the image downloaded then try with the following code

set image for current logged in user

global $user;
$user->picture = $file->fid;
 user_save($user);

else if for other user than

$user = user_load($uid);
$user->picture = $file->fid; 
user_save($user);
| improve this answer | | | | |
1

I think you are getting the user's avatar once the user provides a link. So for that get the image URL and in your function you can do the following.

function custom_user_avatar_save() {
  global $user;
  $uid = $user->uid;
  // Copy the image in tmp directory.
  $user_avatar_path = 'http://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Billy_c.jpg/800px-Billy_c.jpg'; // Change this as variable.
  $filepath = drupal_realpath(file_directory_temp() .  '/' . $uid . '.jpg');

  copy($user_avatar_path, $filepath);

  $account = user_load($uid);

  $file = (object) array(
      'uid' => 1,
      'uri' => $filepath,
      'filemime' => file_get_mimetype($filepath),
      'status' => 1,
    );

  $file = file_copy($file, 'public://');

  $edit['picture'] = $file;
  user_save($account, $edit);
}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.