Answers.Body: <p>With the help of Lars SG's links. I created a, PHP 8, helper function to get a URL based on a file ID provided by the API.</p>
  <pre><code>
  function se_45209_get_entity_image_url( int $entity_id, int $file_id ): string
  {
      if ( $file_id === 0 ) {
          return '';
      }
  
      $file_hash = is_callable( [ 'CRM_Core_BAO_File', 'generateFileHash' ] )
      ? CRM_Core_BAO_File::generateFileHash( $entity_id, $file_id )
      : '';
  
      return CRM_Utils_System::url(
          path: 'civicrm/file',
          query: &quot;reset=1&amp;id={$file_id}&amp;eid={$entity_id}&amp;fcs={$file_hash}&quot;,
          frontend: true
      );
  }
  </code></pre>
  <p><em>Caveat: This does require the CiviCRM plugin to be loaded.</em></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-06T14:04:22.790Z
Answers.LastActivityDate: 2023-07-06T14:04:22.790Z
Answers.UserDisplayName: Michael Bragg
Answers.Body: <p>Short answer: Use the Attachments API instead, but it isn't available in API 4 yet, so you'll have to use API 3.
  See this <a href="https://civicrm.stackexchange.com/questions/28754/get-url-of-custom-image-field">previous question</a> for discussion.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-05T18:21:26.250Z
Answers.LastActivityDate: 2023-07-05T18:21:26.250Z
Answers.UserDisplayName: Lars SG
Body: <p>CiviCRM: v5.57.3 <em>(Currently waiting for a 3rd Party plugin to be updated to be compatible with the latest version.)</em>
  WordPress: v6.2.2</p>
  <p>We have a custom field attached to CiviEvents that lets the team upload an image to be used when outputting the Event on the public front end.</p>
  <p>We are building the front end outside of CiviCRM by using API4 to fetch data.</p>
  <p>We can retrieve the file details via joining to the File on the custom field.</p>
  <pre><code>{
    &quot;id&quot;: 1,
    &quot;Events_.Image&quot;: 1234,
    &quot;file.id&quot;: 1234,
    &quot;file.file_type_id&quot;: null,
    &quot;file.mime_type&quot;: &quot;image/png&quot;,
    &quot;file.uri&quot;: &quot;example.png&quot;,
    &quot;file.document&quot;: null,
    &quot;file.description&quot;: null,
    &quot;file.upload_date&quot;: &quot;2023-07-05 16:00:00&quot;,
    &quot;file.created_id&quot;: null
  </code></pre>
  <p>What we are having issues with is taking this information and getting a full URL to be used inside an <code>&lt;img&gt;</code> tag. eg, <code>https://www.example.test/app/uploads/civicrm/custom/example.png</code>.</p>
  <p>I'd expect a function along the lines of <code>get_image_url(1234);</code> but cannot find anything within the CiviCRM documentation.</p>
  <p>Any guidance is much appreciated.</p>
  
Tags: <wordpress><apiv4><attachments><files><upload>
Title: Get uploaded file full URL via API v4
ThreadText: # Get uploaded file full URL via API v4
  
  (Asked by: username_0 on 2023-07-05)
  
  CiviCRM: v5.57.3 *(Currently waiting for a 3rd Party plugin to be updated to be compatible with the latest version.)*
  WordPress: v6.2.2
  
  We have a custom field attached to CiviEvents that lets the team upload an image to be used when outputting the Event on the public front end.
  
  We are building the front end outside of CiviCRM by using API4 to fetch data.
  
  We can retrieve the file details via joining to the File on the custom field.
  
  ```
  {
    "id": 1,
    "Events_.Image": 1234,
    "file.id": 1234,
    "file.file_type_id": null,
    "file.mime_type": "image/png",
    "file.uri": "example.png",
    "file.document": null,
    "file.description": null,
    "file.upload_date": "2023-07-05 16:00:00",
    "file.created_id": null
  
  ```
  
  What we are having issues with is taking this information and getting a full URL to be used inside an `<img>` tag. eg, `https://www.example.test/app/uploads/civicrm/custom/example.png`.
  
  I'd expect a function along the lines of `get_image_url(1234);` but cannot find anything within the CiviCRM documentation.
  
  Any guidance is much appreciated.
  
  **Tags:** wordpress, apiv4, attachments, files, upload
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2023-07-06. Score: 2 | Accepted answer
  
  With the help of username_1's links. I created a, PHP 8, helper function to get a URL based on a file ID provided by the API.
  
  ```
  
  function se_45209_get_entity_image_url( int $entity_id, int $file_id ): string
  {
      if ( $file_id === 0 ) {
          return '';
      }
  
      $file_hash = is_callable( [ 'CRM_Core_BAO_File', 'generateFileHash' ] )
      ? CRM_Core_BAO_File::generateFileHash( $entity_id, $file_id )
      : '';
  
      return CRM_Utils_System::url(
          path: 'civicrm/file',
          query: "reset=1&id={$file_id}&eid={$entity_id}&fcs={$file_hash}",
          frontend: true
      );
  }
  
  ```
  
  *Caveat: This does require the CiviCRM plugin to be loaded.*
  
  
  ---
  
  ### Answer by username_1 on 2023-07-05. Score: 1
  
  Short answer: Use the Attachments API instead, but it isn't available in API 4 yet, so you'll have to use API 3.
  See this [previous question](https://civicrm.stackexchange.com/questions/28754/get-url-of-custom-image-field) for discussion.
  
  
  ---
  
  