How to crop an image to a rectangle in Salesforce Apex

Image cropping is a useful feature in many situations, but its implementation isn’t always the smoothest when working with Salesforce Apex. To put it bluntly, it’s a quagmire just waiting to suck you in for hours if you are not careful. Instead of dealing with that, how about I show you how to get cropping done fast using an API.

To use our API, first download this zip archive and copy the client folder into your Apex project:

Now we will call our crop function and specify our dimensions and location of the crop:

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>{'left' => 56,'top' => 56,'width' => 56,'height' => 56,'imageFile' => Blob.valueOf('Sample text file\nContents')};try {// cross your fingersBlob result = api.editCropRectangle(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}

And its as easy as that!

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store