I have a Node project that looks at Google Cloud storage and figures out the dimensions of images stored there. This is handy for an API to be able to serve up the right one for the right device. I needed the same capability in Apps Script, but the Nodejs code uses some stuff not available in Apps Script. I could have spun up an htmlservice and done that using the DOM, but since dimensions are buried in the encoding for images I figured it must be possible to play around with an image blob instead. I came across this project https://github.com/tanaikech/ImgApp from Kanshi TANAIKE, which is more extensive than I need, but it did contain the code to extract out the dimensions from various images. It's now in the Images namespace of my cUseful library - 1EbLSESpiGkI3PYmJqWh3-rmLkYKAtCNPi1L2YCtMgo2Ut8xMThfJ41Ex ExamplecUseful.Images.getInfo(blob) will work on any blob containing a supported image, so if that's all you need then you can stop here, but for a more substantial demo of a workflow, I'm going to do all this in a few lines of code.
The test uses 2 images - all exactly the same but in jpg, bmp, gif and png formats. For the full example you'll need these libraries
Setting up service account to access cloud storage
Create the demo.This example does the whole thing starting from this in cloud storage The embedded comments should explain the steps function testImages () { // set up oauth - I'm using a service account with storage admin role // that ive previously set up and called gcs_download const goa = cGoa.make ('gcs_download',PropertiesService.getScriptProperties()); // get a handle for cloud storage // my data is in a bucket called test-bucket // in a folder called testimages const store = new cGcsStore.GcsStore() .setAccessToken(goa.getToken()) .setBucket('test-bucket') .setExpiryLog (false); // get the sheet I'm writing the result to const sheet = SpreadsheetApp .openById('1xxxxxxxxxxx0A') .getSheetByName('testimages'); // get the list of files in the folder const files = store .setFolderKey ('testimages') .getObjects(); // now we have the fully qualified names, we can set the folderkey back to root // filter out any non-images in the folder // and get the blobs and info for each one store.setFolderKey(""); const info = files .filter (function (d) { return d.contentType.match (/^image\//); }) .map (function (d) { return store.get(d.name); }) .map (function (d) { return cUseful.Images.getInfo(d); }); // now write all that to a sheet after clearing the thing // we don't need the blob back so delete that column new cUseful.Fiddler(sheet.clearContents()) .setData (info) .filterColumns (function (name) { return name !== "blob"; }) .dumpValues(); }
|
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >