function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nirav_ShahNirav_Shah 

Send Profile Photo in base64 Format

I have created API (an apex class with restful service - HttpGet) 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?
Best Answer chosen by Nirav_Shah
Nirav_ShahNirav_Shah
Hi Ashish,
The above methods work fine, but I have used following to get the Profile Photo in Base64 format.

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); 

System.debug(base64);
Using this we don't need to hit API.

Hope this will help others

Thanks
Nirav

 

All Answers

Ashish KumarAshish Kumar

Hi Nirav,

You will have to do a HTTP GET callout to get the profile image and convert it into base64 format.

Please take reference from below code snippet.
 

lstUser = new List<User>();
        lstUser = [Select SmallPhotoUrl, FullPhotoUrl From User where Id =: UserInfo.getUserId()];
        profilePicURL = lstUser[0].FullPhotoUrl;	
		// Create a link to the file.
		String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm() +   '/' + profilePicURL;
		getProfile(fullFileURL);

///////////////////////// Method to get image from url and convert it into base64 format
		public static String getProfile(String url) {
	        HttpRequest req = new HttpRequest();
	        HttpResponse res = new HttpResponse();
	        Http http = new Http();
			
	        req.setEndpoint(fullFileURL);
	        req.setMethod('GET');
	        req.setCompressed(true); 
        try {
            res = http.send(req);
			system.debug(res.getBody());
			system.debug(res.getBodyAsBlob());
			String picInBase64 = EncodingUtil.base64Encode(res.getBodyAsBlob());
			return picInBase64;
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }

    }

Please let me know if it helps.

Regards,
Ashish Kr.
Nirav_ShahNirav_Shah
Hi Ashish,

Thanks a lot, this is the exactly what I needed.
 
Nirav_ShahNirav_Shah
Hi Ashish,
The above methods work fine, but I have used following to get the Profile Photo in Base64 format.

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); 

System.debug(base64);
Using this we don't need to hit API.

Hope this will help others

Thanks
Nirav

 
This was selected as the best answer