How to remove transparency from an image in Salesforce Apex
Removing transparency from an image is a useful feature to have, especially when non-white backgrounds are giving you trouble. Generally speaking, this is a matter of rendering the image over a white background layer. While this should be a simple endeavor, it is surprisingly difficult to achieve in Apex, for some reason. So that’s why we are here today, to make it simple again.
So let’s start by installing our client. This can be done manually by downloading this zip archive and extracting the /client folder into your project in Apex.
https://github.com/Cloudmersive/Cloudmersive.APIClient.Apex.ImageRecognition/archive/master.zip
Now we shall call our function for removing transparency:
SwagEditApi api = new SwagEditApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'imageFile' => Blob.valueOf('Sample text file\nContents')};try {// cross your fingersBlob result = api.editRemoveTransparency(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}
And now you have your image back again, sans-transparency!