I\'m using the google sheets scripting language, and I want to get the ratio of an image from a web link, e.g. this flag.
I have seen a way to do it here, but I wan
How about this workaround? I think that there are several answers for your situation. So please think of this as one of them. The flow of this script is as follows.
In order to use this sample script, please enable Drive API at Advanced Google Services and API console as follows.
function myFunction(url) {
var blob = UrlFetchApp.fetch(url).getBlob();
var fileId = DriveApp.createFile(blob).getId();
var imageMediaMetadata = Drive.Files.get(fileId).imageMediaMetadata;
var aspectRatio = imageMediaMetadata.width / imageMediaMetadata.height;
Drive.Files.remove(fileId);
return aspectRatio;
}
https://www.theodora.com/flags/e_timor.gif
in your question is used for this script, 1.5
is retrieved.If this was not what you want, I'm sorry.
If you don't want to create files in Google Drive, you can retrieve the aspect ratio using this library. This library can retrieve the image size from a blob, because the binary data of the image is parsed. When this library is used for your situation, the script becomes as follows.
function myFunction(url) {
var blob = UrlFetchApp.fetch(url).getBlob();
var res = ImgApp.getSize(blob);
var aspectRatio = res.width / res.height;
return aspectRatio;
}