Airtable Scripts to resize and crop images

PHOTO EMBED

Sat Jul 16 2022 00:22:49 GMT+0000 (Coordinated Universal Time)

Saved by @mikecardona #javascript

//Trigger: User clicks button
// settings you change:
// ========================
let apiKey = "yourApiKeyHere";
let apiSecret = "yourSecretHere";
let originalFieldName = "Original";
let largeFieldName = "Large Web";
let largeWidth = 630;
let smallFieldName = "Small Web";
let smallWidth = 416;

// ========================
// Image resize code
// ========================

let table = base.getTable("Employees");
let record = await input.recordAsync("Select an Employee whose images you would like to create", table); 

// need an image there
if(record.getCellValue(originalFieldName) === null){	
		//console.log("Please upload an image for this record");
    output.text("Please upload an image for this record");
    return;
}

let imageToResizeUrl = record.getCellValue(originalFieldName)[0].url;

// send stuff to image processor
let callToKraken = await remoteFetchAsync('https://api.kraken.io/v1/url',
    {
        method: 'POST',
        body: JSON.stringify(
            {
                "auth": {
                    "api_key": apiKey,
                    "api_secret": apiSecret
                },
                "wait": true,
                "lossy": true,
                "url": imageToResizeUrl,
                "resize": [
                    {
                        "id": "small",
                        "strategy": "landscape",
                        "width": smallWidth,
                    },
                    {
                        "id": "large",
                        "strategy": "landscape",
                        "width": largeWidth,
                    }
                ]
            }       
        ),
        headers:{
            'Content-type':'application/json',
            'Accept':'application/json'
        }
    }
)
let returnedImages = await callToKraken.json();
// some debugging checks. Uncomment to view
//console.log(returnedImages); 
//console.log(returnedImages.results.small.kraked_url);

//add images as attachments into Airtable row:
// "small" and "large" under "results" below are the IDs we specified in the Kraken body above
await table.updateRecordAsync(record, {
    [smallFieldName]: [ { url: returnedImages.results.small.kraked_url } ],
    [largeFieldName]: [ { url: returnedImages.results.large.kraked_url } ]
})
content_copyCOPY

https://www.nocodecollab.com/tutorial/airtable-scripts-to-resize-and-crop-images