- You want to directly retrieve the width and height from the blob of image data using Google Apps Script.
If my understanding is correct, how about this answer?
Pattern 1:
In this pattern, the width and height are retrieved from the blob of image data using Google Apps Script.
Unfortunately, in the current stage, there are no methods for directly retrieving the width and height from the blob of image data. So I created the script for retrieving by analyzing the binary level as a GAS library. This is also mentioned by Cooper's comment. Using this library, you can directly retrieve the width and height from the blob of image data using Google Apps Script.
Sample script:
About the install of the library, please check here.
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = ImgApp.getSize(fileBlob);
Logger.log(res)
Result:
{
identification : ### BMP, GIF, PNG and JPG ###,
width : ### pixel ###,
height : ### pixel ###,
filesize : ### bytes ###
}
- In this library, the width and height can be retrieved from BMP, GIF, PNG and JPG.
Pattern 2:
In this pattern, the width and height are retrieved using a Google Document. In this case, please enable Drive API at Advanced Google services.
Sample script:
function getSize_doc(blob) {
var docfile = Drive.Files.insert({
title: "temp",
mimeType: "application/vnd.google-apps.document",
}).getId();
var img = DocumentApp.openById(docfile).insertImage(0, blob);
Drive.Files.remove(docfile);
return {width: img.getWidth(), height: img.getHeight()};
}
function main() {
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = getSize_doc(fileBlob);
Logger.log(res)
}
- In this script, at first, Google Document is created as a temporal file, and the width and height are retrieved from the inserted image from the blob. Then, the temporal file is deleted.
Pattern 3:
In this pattern, the width and height are retrieved by creating the blob as a file. In this case, please enable Drive API at Advanced Google services.
Sample script:
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var res = Drive.Files.insert({title: "temp"}, fileBlob);
Drive.Files.remove(res.id);
Logger.log(res.imageMediaMetadata.width)
Logger.log(res.imageMediaMetadata.height)
- In this script, at first, a file is created from the blob as a temporal file, and the width and height are retrieved from the created file. Then, the temporal file is deleted.
References:
If this answer was not the direction you want, I apologize.