Answers.Body: <p>Contact Image URL is directly stored in <code>civicrm_contact</code> table, so I think if you have the URL which can display an image, you should be directly able to use the contact create API as below -</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'create', array(
    'contact_type' =&gt; "Individual",
    'first_name' =&gt; "first",
    'last_name' =&gt; "last",
    'image_URL' =&gt; "&lt;url&gt;",
  ));
  </code></pre>
  
  <p>If you're on version &lt; 5.0, the contact summary page will render the image only iff the URL is in form of <code>http://site-name/civicrm/contact/imagefile?photo=imageName.jpg</code> and your image file should be present in your custom file upload directory.</p>
  
  <p>There is now a <a href="https://github.com/civicrm/civicrm-core/pull/11681" rel="noreferrer">patch(included in version 5.0)</a> which allows the contact summary page to display any URL image irrespective of the <code>photo</code> param in the URL.</p>
  
  <p>If you want to download a copy of a remote image to your local file system and use that, you could do something along the lines of:</p>
  
  <pre><code>$contactId = 123;
  $imageName = 'jitendra.jpg';
  $path = Civi::paths()-&gt;getPath(Civi::settings()-&gt;get('customFileUploadDir')) . $imageName;
  file_put_contents($path, file_get_contents('https://i.sstatic.net/yjnic.jpg'));
  civicrm_api3('Contact', 'create', [
    'id' =&gt; $contactId,
    'image_URL' =&gt; CRM_Utils_System::url('civicrm/contact/imagefile', ['photo' =&gt; $imageName], true)
  ]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T11:03:07.213Z
Answers.Text: thanks for the answer, Jitendra, very helpful. I still have one outstanding question though. How would I upload an image file so that it is available at http://site-name/civicrm/contact/imagefile?photo=imageName.jpg?
Answers.UserDisplayName: michaelmcandrew
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T11:10:32.497Z
Answers.Text: Also, I played around with file uploading via the UI and the image URL was reported as `http://chatbot.buildkit:8080/civicrm/file?reset=1&filename=michael_ed7e440cde7726911615c9e97042b177.png&mime-type=image/png`. I was expecting something similar to `http://site-name/civicrm/contact/imagefile...` - any thoughts on that?
Answers.UserDisplayName: michaelmcandrew
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T11:54:09.880Z
Answers.Text: May I know the CiviCRM version you are testing on? I guess contact upload uses that URL in the older versions.
Answers.UserDisplayName: jitendra - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T11:55:56.930Z
Answers.Text: If you don't have the image file available in the custom upload directory - you can apply the PR mentioned in the answer and fed the URL to the image_url param in the API. Note the URL should be able to display an image.
Answers.UserDisplayName: jitendra - fuzion
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T15:50:13.243Z
Answers.Text: OK, I think I get it now. I have added an example to your answer to clarify my particular use case. Do you want to have a look and let me know what you think?
Answers.UserDisplayName: michaelmcandrew
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T23:04:02.063Z
Answers.Text: PS I was on 4.7.32 / 5.0
Answers.UserDisplayName: michaelmcandrew
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-21T09:14:53.033Z
Answers.LastActivityDate: 2018-03-21T15:49:54.590Z
Answers.LastEditDate: 2018-03-21T15:49:54.590Z
Answers.UserDisplayName: jitendra - fuzion
Body: <p>Can someone give me a simple example of how to upload a Contact image via the API? I am guessing it will involve a call to the File API as well but not sure.</p>
  
  <p>I have the original image as a URL but want to store it in CiviCRM (not just reference the URL).</p>
  
  <p>Bonus points for a nicely commented answer :)</p>
  
  <p>Thanks!</p>
  
Tags: <contacts><api><images><files>
Title: Upload a contact image via the API
ThreadText: # Upload a contact image via the API
  
  (Asked by: username_0 on 2018-03-19)
  
  Can someone give me a simple example of how to upload a Contact image via the API? I am guessing it will involve a call to the File API as well but not sure.
  
  I have the original image as a URL but want to store it in CiviCRM (not just reference the URL).
  
  Bonus points for a nicely commented answer :)
  
  Thanks!
  
  **Tags:** contacts, api, images, files
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-21. Score: 6 | Accepted answer
  
  Contact Image URL is directly stored in `civicrm_contact` table, so I think if you have the URL which can display an image, you should be directly able to use the contact create API as below -
  
  ```
  $result = civicrm_api3('Contact', 'create', array(
    'contact_type' => "Individual",
    'first_name' => "first",
    'last_name' => "last",
    'image_URL' => "<url>",
  ));
  
  ```
  
  If you're on version < 5.0, the contact summary page will render the image only iff the URL is in form of `http://site-name/civicrm/contact/imagefile?photo=imageName.jpg` and your image file should be present in your custom file upload directory.
  
  There is now a [patch(included in version 5.0)](https://github.com/civicrm/civicrm-core/pull/11681) which allows the contact summary page to display any URL image irrespective of the `photo` param in the URL.
  
  If you want to download a copy of a remote image to your local file system and use that, you could do something along the lines of:
  
  ```
  $contactId = 123;
  $imageName = 'username_1.jpg';
  $path = Civi::paths()->getPath(Civi::settings()->get('customFileUploadDir')) . $imageName;
  file_put_contents($path, file_get_contents('https://i.sstatic.net/yjnic.jpg'));
  civicrm_api3('Contact', 'create', [
    'id' => $contactId,
    'image_URL' => CRM_Utils_System::url('civicrm/contact/imagefile', ['photo' => $imageName], true)
  ]);
  
  ```
  
  > **Comments:**
  > **username_0**: thanks for the answer, username_1, very helpful. I still have one outstanding question though. How would I upload an image file so that it is available at http://site-name/civicrm/contact/imagefile?photo=imageName.jpg?  
  > **username_0**: Also, I played around with file uploading via the UI and the image URL was reported as `http://chatbot.buildkit:8080/civicrm/file?reset=1&filename=michael_ed7e440cde7726911615c9e97042b177.png&mime-type=image/png`. I was expecting something similar to `http://site-name/civicrm/contact/imagefile...` - any thoughts on that?  
  > **username_1**: May I know the CiviCRM version you are testing on? I guess contact upload uses that URL in the older versions.  
  > **username_1**: If you don't have the image file available in the custom upload directory - you can apply the PR mentioned in the answer and fed the URL to the image_url param in the API. Note the URL should be able to display an image.  
  > **username_0**: OK, I think I get it now. I have added an example to your answer to clarify my particular use case. Do you want to have a look and let me know what you think?  
  > **username_0**: PS I was on 4.7.32 / 5.0  
  
  ---
  
  