I have a blob containing an image that is remotely loaded and created in Google Drive:
var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.get
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Unfortunately, in the current stage, there are no methods for resizing directly the image in Google Apps Script. But there is a workaround. The flow of this workaround is as follows.
thumbnailLink
is retrieved.thumbnailLink
.
thumbnailLink
is like https://lh3.googleusercontent.com/###=s220
.=s220
is changed, the size of thumbnail is also changed. This workaround uses this.Before you run the script, please set the variables of width
, outputFilename
and url
. And also, please enable Drive API at Advanced Google services.
function myFunction() {
var width = 10; // Please set the size of width with the unit of pixels.
var outputFilename = "sample.png"; // Please set the output filename.
var url = "###";
var blob1 = UrlFetchApp.fetch(url).getBlob().setName("sampleImage_temporal");
var fileId = DriveApp.createFile(blob1).getId();
var link = Drive.Files.get(fileId).thumbnailLink.replace(/\=s.+/, "=s" + width);
var blob2 = UrlFetchApp.fetch(link).getBlob().setName(outputFilename);
var file = DriveApp.createFile(blob2);
Drive.Files.remove(fileId);
}
image/png
.If this was not the direction you want, I apologize.
There isn't any API call to resize images on apps script, you could use a third party service like tinypng or some others as there isn't nothing native on apps script.
Images in Apps Scripts are resized by grabbing the blob from the request and placing it in whatever app you're using with a width
or height
attribute (these scale both width and height of the image, just use whichever is most useful for you).
It looks like you're trying to resize an image on its way to a drive folder though, that should be done using a server/serverless function (maybe look into a Node-based resize tool that you can shove into a Google Cloud Function?).
You can also try to create a Google Doc and paste the image into the doc, then get the image blob and save it into your folder.