One of my clients, takes many photos as part of their data entry process using a Canvas app. We need to compress the photos as the collected data, including the photos, is used to generate a PDF. This PDF generation was timing out every now and then.
How to compress photos
Table of Contents
In this solution a Power Automate flow is used to compress the images. We do have premium licences however we wanted to avoid using any 3rd party connectors to reduce the image sizes.
My uploaded photos exist within a SharePoint library. Using a Get items action I’m collecting the images that I’m interested in. Notice that you might have to filter your data. Please have a look at SharePoint odata filter query in Get items actions in Power Automate for details on how to filter your data.

We can now add an Apply to each to our flow that will read the data form our Get items action and process each item found.
You can use the outputs function
outputs('Get_items')?['body/value']
or the body function to process all the items found.
body('Get_items')?['value']
Inside the Apply to each we will need the following steps:
- Compose
- Create file
- Get file Thumbnail
- HTTP
- Compose
- Delete file
When we added the above action to our flow we should have something that looks like this:

We’re copying the data to a OneDrive library as this generates Thumbnails for the photos instantly. The SharePoint library doesn’t give this to us.
First we will look at the Content step, this is a Compose action, that constructs the file in the right format:
{
"$content-type": "@{last(split(first(split(items('Apply_to_each')?['Base64'],';base64')),':'))}",
"$content": "@{last(split(items('Apply_to_each')?['Base64'],';base64,'))}"
}
Ok, those expressions might need some explaining.
The Get Items will return a property that contains the Base64 code of our photo. The OneDrive action to create a file however will want data to be supplied as $content-type and $content. Using the Base64 property and splitting it on the “;base64,” text will give us the data needed.
"Base64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgM"
Now we can create a file in OneDrive using the output of the above Compose action.

And then the Get file thumbnail returns a copy of the photo at a reduced size. I’m selecting Large as the size, as I don’t want to reduce the quality of the image too much. This Large Thumbnail gives a good enough image for this solution.

But we are not there yet.
The Get the thumbnail action gives us a URL rather than file content.

Now we could of course save a file using the path however there is an easier way and then collect the content in the format that we want it, however there is an easier way. Also when I attempted this, in order to avoid the premium HTTP connector, the OneDrive connector would throw 429 errors (throttling errors). The OneDrive connector would simply not be able to keep up without me building in delays into the flow.
The HTTP action will give us the content of the Thumbnail that we are after.

Now we can get the Thumbnail content from the Body of the above HTTP action and we are nearly done. You could now use Compose actions inside apply to each steps in Power Automate to collect an array of Thumbnails.

We just need to clean up the file in OneDrive and we have our Thumbnails generated.

Discover more from SharePains
Subscribe to get the latest posts sent to your email.