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 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

Example

cUseful.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.

  • Use a service account to authenticate to Google Cloud Storage, using my goa library.
  • Get list of objects in a cloud storage folder, and get their contents as a blob using my gcsstore library.
  • Get info about the images using the new name space (Images) in my cuseful library
  • Populate a sheet with info about each of the found images.

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

  • cUseful – 1EbLSESpiGkI3PYmJqWh3-rmLkYKAtCNPi1L2YCtMgo2Ut8xMThfJ41Ex
  • cGoa – 1v_l4xN3ICa0lAW315NQEzAHPSoNiFdWHsMEwj2qA5t9cgZ5VWci2Qxv2
  • cGcsStore – 1w0dgijlIMA_o5p63ajzcaa_LJeUMYnrrSgfOzLKHesKZJqDCzw36qorl

Setting up service account to access cloud storage

  • Create a service account for the project containing the cloud storage bucket you’ll use, give it storage admin role, download the .json credentials to Drive and grab the file ID
  • Run this code to register the service account in your script, substituting your drive ID. You can delete it once you’ve run it once.

Create the demo.

This example does the whole thing starting from this in cloud storage

and finishing with this in a sheet

The embedded comments should explain the steps

For more like this see Google Apps Scripts Snippets

For help and more information join our community, follow the blog or follow me on twitter.