/airtable-script-compress-img.js Secret
Created
December 19, 2022 17:50
Star
You must be signed in to star a gist
Compress Images w/ Airtable Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Script settings | |
// Allow user to select this to be used across several tables with the same view and image field name | |
const config = input.config({ | |
title: "Compress Images", | |
description: "Compress and convert all images to webp and 2000px wide.", | |
items: [ | |
input.config.table("tableSelect", { | |
label: "Table", | |
}), | |
input.config.view("viewSelect", { | |
label: "View", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("imageSelect", { | |
label: "Image Field", | |
description: "Attachment field where the screenshot is stored.", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("imageSizeSelect", { | |
label: "Image Size Field", | |
description: "Number field that gets updated with the new compressed size.", | |
parentTable: "tableSelect", | |
}), | |
input.config.field("imageErrorSelect", { | |
label: "Image Error Field", | |
description: "Checkbox field that gets checked if there is a compression error.", | |
parentTable: "tableSelect", | |
}), | |
], | |
}); | |
// API Key and Secret | |
let apiKey = "ENTER YOUR KRAKEN API KEY HERE"; | |
let apiSecret = "ENTER YOUR KRAKEN API SECRET HERE"; | |
// Set settings to variables | |
const configTable = config.tableSelect.name; | |
const configView = config.viewSelect.name; | |
const configImage = config["imageSelect"].name; | |
const configImageSize = config["imageSizeSelect"].name; | |
const configImageError = config["imageErrorSelect"].name; | |
// Set table and view based on settings | |
const table = base.getTable(configTable); | |
const view = table.getView(configView); | |
// Get all records by Name field | |
let query = await view.selectRecordsAsync({ fields: [configImage, "Name"] }); | |
// Loop through each record to compress image | |
for (let record of query.records) { | |
// Get the record name | |
let imageRecordName = record.getCellValue("Name"); | |
// Get the URL of attachment field | |
let imageToResizeUrl = record.getCellValue(configImage)[0].url; | |
// Send stuff to Kraken for compression | |
let callToKraken = await remoteFetchAsync("https://api.kraken.io/v1/url", { | |
method: "POST", | |
body: JSON.stringify({ | |
auth: { | |
api_key: apiKey, | |
api_secret: apiSecret, | |
}, | |
// "dev": true, | |
url: imageToResizeUrl, | |
wait: true, | |
lossy: true, | |
webp: true, //convert to webp | |
resize: [ | |
{ | |
id: "compressed", | |
strategy: "landscape", | |
width: 2000, //resize max width of compressed image | |
}, | |
], | |
}), | |
headers: { | |
"Content-type": "application/json", | |
Accept: "application/json", | |
}, | |
}); | |
let returnedImages = await callToKraken.json(); | |
// If Kraken throws an error message, then continue to compress the next image | |
if (!callToKraken.ok) { | |
// Console log the error message | |
console.error(callToKraken); | |
// Check the error checkbox field | |
await table.updateRecordAsync(record, { | |
[configImageError]: true, | |
}); | |
// Continue to next image | |
continue; | |
} else { | |
// Update the image field with the new compressed image | |
await table.updateRecordAsync(record, { | |
[configImage]: [{ url: returnedImages.results.compressed.kraked_url }], | |
[configImageSize]: returnedImages.results.compressed.kraked_size / 1000, | |
}); | |
// Console log the image that was compressed and its new size | |
console.log("Compressed image to " + returnedImages.results.compressed.kraked_size / 1000 + "KB (" + imageRecordName + ")"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment