I have created API to expose the User Details to an external system in which I am sending all the details regarding users like Name, Email, Language etc. Now I want to send the Profile Photo also, I know we can query and get SmallPhotoUrl and FullPhotoUrl but I don't want URL. I want the actual image in Base64 format.

Any Ideas for this?

  • If you have an apex service, would you want to make a rest callout to salesforce API to return the blob of photo URL? – Raul Mar 20 '18 at 10:13
  • @RahulSharma Sorry I didn't get you. What I need to do is I want to give the profile photo in a base64 format so they can directly convert it and use it – Nirav Shah Mar 20 '18 at 10:20
  • 1
    What is your API, an apex class with restful service? – Raul Mar 20 '18 at 10:24
  • yes it is.... @HttpGet – Nirav Shah Mar 20 '18 at 10:29
  • I have created the class with restful service HttpGet – Nirav Shah Mar 20 '18 at 10:34

Here's an easy 4 step snippet to get a profile photo as a base64 image:

// 1. Query for user info 
User u = [Select SmallPhotoUrl, FullPhotoUrl From User where id = :Userinfo.getUserid()];

// 2. "Navigate" to the page with the image 
PageReference ref = new PageReference(u.FullPhotoUrl); 

// 3. Get the content of that page (the image) as a blob
Blob b = ref.getContent();

// 4. Convert it to base64   
String base64 = EncodingUtil.base64Encode(b); 


Currently working on something very close to this, will update this post with any findings.

| 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.