Question: I use the following script to extract the image name and image URL of all the images inside a google drive folder.
It extracts this info into a google sheets doc. Is it possible to add the image dimensions (image height and width) into the script so it pulls into the doc aswell?

Script:

```
// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
  var foldername = ‘FolderName1’;
  var folderlisting = 'listing of folder ' + foldername;

  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();

  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['name', 'link'] );

  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();

    sheet.appendRow( [name, link] );     
  }  
};

```

Answer: 
Explanation:
------------

Your goal is to retrieve the `width` and the `height` of the pictures that are stored in a specific folder in Google Drive.

* This info is located in [imageMediaMetadata](https://developers.google.com/drive/api/v2/reference/files) in the properties `imageMediaMetadata.width` and `imageMediaMetadata.height`.
* To retrieve this metadata you need to enable the Drive API and pass the `fileId` of your file as an argument:

```
Drive.Files.get(fileId).imageMediaMetadata;

```
* Also store the data in an array and set the values directly to the sheet so you won't have to use `appendRow` iteratively which is very inefficient.

Last but not least, it is a good idea to check whether the file is an `image` regardless of each extension (`jpeg`,`png`). Otherwise, you will get an error if you the folder contains other types of files (spreadsheet documents etc.).

You can use [includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) to check if the [getMimeType](https://developers.google.com/apps-script/reference/drive/file#getmimetype) contains the word `image`:

```
file.getMimeType().includes("image")

```

and only for these files, find the `width` and `height`.

Solution:
---------

```
function listFolderContents() {
  var foldername = 'FolderName1';
  var folderlisting = 'listing of folder ' + foldername;
  var folders = DriveApp.getFoldersByName(foldername);
  var folder = folders.next();
  var contents = folder.getFiles();
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  var data = [['name', 'link', 'width', 'height']];

  while(contents.hasNext()) {
    let file = contents.next();
    if( file.getMimeType().includes("image") ){
      let name = file.getName();
      let link = file.getUrl();
      let fileId = file.getId();
      let dims = Drive.Files.get(fileId, {supportsAllDrives: true}).imageMediaMetadata
      let width = dims.width;
      let height = dims.height;

    data.push( [name, link, width, height] );
    }     
  }  
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);
};

```

**Be careful!**

Make sure to **enable** `Drive API` from:

* **Resources** => **Advanced Google Services** (Legacy editor)
* **Services** (New Editor)
You may use the [ImgApp library](https://github.com/tanaikech/ImgApp) to retrieve the width and height of any image file in Google Drive.

```js
var blob = DriveApp.getFileById(fileId).getBlob();
var res = ImgApp.getSize(blob);
var width = res.width;
var height = res.height;

```