Answers.Body: <p>I think I've figured this out and it seems to work reliably in D8.5.4/CV4.7.30.</p>
  
  <p>The basic approach is to:</p>
  
  <ol>
  <li>Lookup the Civi contact based on the Drupal user id</li>
  <li>Make a copy of the uploaded account pic (Civi can't share, and requires the file to be in the custom uploads dir)</li>
  <li>Update the image_URL of the Civi contact using the Civi v3 api</li>
  </ol>
  
  <p>You MUST use the image_URL value expected by the Civi code, which is specific to its function.  It looks like this...</p>
  
  <p><code>http://www.mydrupalsite.com/civicrm/contact/imagefile?photo=FILENAME.jpg</code></p>
  
  <p>...and the file must be in the CiviCRM custom uploads folder.  I see no alternatives to this in the code and the docs are completely silent on it.  And, yes, I'm aware that this is a FULLY QUALIFIED URL being stored in the database.  Whoever chose this technique was either highly constrained by legacy code or smoking crack.  I've tried relative URLs and they don't seem to work.</p>
  
  <p>I've created a Github gist here that demonstrates the approach (use at your own risk):</p>
  
  <p><a href="https://gist.github.com/jptillman/ba3f29fd82cc83f3f1de0d807b50c843" rel="nofollow noreferrer">https://gist.github.com/jptillman/ba3f29fd82cc83f3f1de0d807b50c843</a></p>
  
  <p>In my site using this code, removing the Drupal image also removes the Civi image from the Contact record, but I'm fairly sure that Civi isn't cleaning up, so this will leave orphaned files behind in the custom uploads folder.  Probably wouldn't be too hard to engineer a delete of the file somehow.</p>
  
  <p>NOTE:  This code will overwrite any existing picture in the Civi record.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T15:00:33.987Z
Answers.LastActivityDate: 2018-06-21T15:00:33.987Z
Answers.UserDisplayName: theta-fish
Answers.Body: <p>I am not sure how to sync drupal profile image with CiviCRM contact image through UI. But you can add module and invoke hook to update the contact image.</p>
  
  <p>Here is the snippet how to add image url to contact</p>
  
  <pre><code>$params = [
    'contact_id' =&gt; 2,
    'image_URL' =&gt; [
      'name' =&gt; '/var/www/html/drupal/sites/default/files/civicrm/custom/Screenshot_from_2018_06_19_15_46_08_1877c5451ed2e68a12816071e0006fef.png',
      'type' =&gt; 'image/png',
    ],
  ];
  CRM_Contact_BAO_Contact::processImageParams($params);
  civicrm_api3('Contact', 'create', $params);
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T13:13:44.087Z
Answers.Text: This is incorrect, at least for Civi 4.7.30.  The way you provide the image_URL above, it will get persisted in the database field as `"Array"` (the literal word) and be useless.  Even if you provide the full filesystem path string for `image_URL`, this will still not work, because it is not considered a valid URL by the Civi code.  `image_URL` has to be a specially formatted FULLY QUALIFIED URL (!?) to work or it will simply be displayed as `"Custom"` in the display and you won't see the image.
Answers.UserDisplayName: theta-fish
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T13:24:35.707Z
Answers.Text: Did you try the above snippet or you guessing it as IT WILL NEVER WORK!!
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T15:33:40.913Z
Answers.Text: Actually, yes, I did.  The record ended up with 'Array' in the field, as I stated.  Also, you obviously never ran it, because there is a syntax error in the last line of code.
Answers.UserDisplayName: theta-fish
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T15:44:50.533Z
Answers.Text: Well, drat, I stand corrected.  In fixing your typo in the last line, I apparently accidentally removed the call to processImageParams() which I see now converts the filesystem path to the appropriate URL.  That'll teach me to make sure I've correctly run the provided example before critiquing.
Answers.UserDisplayName: theta-fish
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T16:15:35.127Z
Answers.Text: I've incorporate your processImageParams() approach into the gist I linked to in my answer.  Your helpful response removed some complexity and hard-coded values.  I appreciate your assistance.
Answers.UserDisplayName: theta-fish
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T16:18:19.710Z
Answers.Text: Congrats to get it working and thanks for sharing module code. Cheerz!!
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-20T12:21:47.370Z
Answers.LastActivityDate: 2018-06-21T15:40:34.267Z
Answers.LastEditDate: 2018-06-21T15:40:34.267Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I'm trying to find a way to automatically update the CiviCRM contact image when a user updates their Drupal profile image.  I'm specifically looking for Drupal8.5/Civi4.7 but I'll take anything that can properly update Civi4.7 at this point, regardless of Drupal version.  It seems that the Drupal/Civi API functions are almost, but not quite, capable of making this easy, although it may just be a documentation deficiency -- I'm still digging in the code.</p>
  
  <p>I found this discussion on the old Civi forum (circa 2011) which discusses it.
  <a href="https://forum.civicrm.org/index.php%3Ftopic=22405.0.html" rel="nofollow noreferrer">https://forum.civicrm.org/index.php%3Ftopic=22405.0.html</a></p>
  
  <p>There is also some old code in the Civi site which directly updates the database and also doesn't seem to work anymore:
  <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/Adding+pictures+in+civicrm+profile+page" rel="nofollow noreferrer">https://wiki.civicrm.org/confluence/display/CRMDOC/Adding+pictures+in+civicrm+profile+page</a></p>
  
  <p>Is there anything as of Civi 4.7.30 which can do this?</p>
  
  <p>If not, can someone point me to some docs/examples that demonstrate how to programmatically add a photo image to a contact in Civi?</p>
  
Tags: <drupal><contacts><drupal-8><contact-image>
Title: How can I sync the Drupal User Profile image and the CiviCRM contact image?
ThreadText: # How can I sync the Drupal User Profile image and the CiviCRM contact image?
  
  (Asked by: username_0 on 2018-06-20)
  
  I'm trying to find a way to automatically update the CiviCRM contact image when a user updates their Drupal profile image. I'm specifically looking for Drupal8.5/Civi4.7 but I'll take anything that can properly update Civi4.7 at this point, regardless of Drupal version. It seems that the Drupal/Civi API functions are almost, but not quite, capable of making this easy, although it may just be a documentation deficiency -- I'm still digging in the code.
  
  I found this discussion on the old Civi forum (circa 2011) which discusses it.
  <https://forum.civicrm.org/index.php%3Ftopic=22405.0.html>
  
  There is also some old code in the Civi site which directly updates the database and also doesn't seem to work anymore:
  <https://wiki.civicrm.org/confluence/display/CRMDOC/Adding+pictures+in+civicrm+profile+page>
  
  Is there anything as of Civi 4.7.30 which can do this?
  
  If not, can someone point me to some docs/examples that demonstrate how to programmatically add a photo image to a contact in Civi?
  
  **Tags:** drupal, contacts, drupal-8, contact-image
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-06-21. Score: 1 | Accepted answer
  
  I think I've figured this out and it seems to work reliably in D8.5.4/CV4.7.30.
  
  The basic approach is to:
  
  1. Lookup the Civi contact based on the Drupal user id
  2. Make a copy of the uploaded account pic (Civi can't share, and requires the file to be in the custom uploads dir)
  3. Update the image\_URL of the Civi contact using the Civi v3 api
  
  You MUST use the image\_URL value expected by the Civi code, which is specific to its function. It looks like this...
  
  `http://www.mydrupalsite.com/civicrm/contact/imagefile?photo=FILENAME.jpg`
  
  ...and the file must be in the CiviCRM custom uploads folder. I see no alternatives to this in the code and the docs are completely silent on it. And, yes, I'm aware that this is a FULLY QUALIFIED URL being stored in the database. Whoever chose this technique was either highly constrained by legacy code or smoking crack. I've tried relative URLs and they don't seem to work.
  
  I've created a Github gist here that demonstrates the approach (use at your own risk):
  
  <https://gist.github.com/jptillman/ba3f29fd82cc83f3f1de0d807b50c843>
  
  In my site using this code, removing the Drupal image also removes the Civi image from the Contact record, but I'm fairly sure that Civi isn't cleaning up, so this will leave orphaned files behind in the custom uploads folder. Probably wouldn't be too hard to engineer a delete of the file somehow.
  
  NOTE: This code will overwrite any existing picture in the Civi record.
  
  
  ---
  
  ### Answer by username_1 on 2018-06-20. Score: 2
  
  I am not sure how to sync drupal profile image with CiviCRM contact image through UI. But you can add module and invoke hook to update the contact image.
  Here is the snippet how to add image url to contact
  $params = [
    'contact_id' => 2,
    'image_URL' => [
      'name' => '/var/www/html/drupal/sites/default/files/civicrm/custom/Screenshot_from_2018_06_19_15_46_08_1877c5451ed2e68a12816071e0006fef.png',
      'type' => 'image/png',
    ],
  ];
  CRM_Contact_BAO_Contact::processImageParams($params);
  civicrm_api3('Contact', 'create', $params);
  
  HTH
  Pradeep
  
  > **Comments:**
  > **username_0**: This is incorrect, at least for Civi 4.7.30.  The way you provide the image_URL above, it will get persisted in the database field as `"Array"` (the literal word) and be useless.  Even if you provide the full filesystem path string for `image_URL`, this will still not work, because it is not considered a valid URL by the Civi code.  `image_URL` has to be a specially formatted FULLY QUALIFIED URL (!?) to work or it will simply be displayed as `"Custom"` in the display and you won't see the image.  
  > **username_1**: Did you try the above snippet or you guessing it as IT WILL NEVER WORK!!  
  > **username_0**: Actually, yes, I did.  The record ended up with 'Array' in the field, as I stated.  Also, you obviously never ran it, because there is a syntax error in the last line of code.  
  > **username_0**: Well, drat, I stand corrected.  In fixing your typo in the last line, I apparently accidentally removed the call to processImageParams() which I see now converts the filesystem path to the appropriate URL.  That'll teach me to make sure I've correctly run the provided example before critiquing.  
  > **username_0**: I've incorporate your processImageParams() approach into the gist I linked to in my answer.  Your helpful response removed some complexity and hard-coded values.  I appreciate your assistance.  
  > **username_1**: Congrats to get it working and thanks for sharing module code. Cheerz!!  
  
  ---
  
  