3

I have a blob containing an image that is remotely loaded and created in Google Drive:

var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var folder = DriveApp.getFolderById('THjgj698979XXXXXXXX');
var result = folder.createFile(fileBlob);

What I want is to resize the image contained in this fileBlob. How can I do that?

For your information, a post that can help but finally didn't really help me:(explains how to resize an image, not how to resize an image in a blob) Resizing image in Google Apps Script

Thanks

EDIT :

Here is what I do now. But I have got a problem : the image is scaled down but not at the size I specify...

 var response = UrlFetchApp.fetch(fileURL);
    var fileBlob = response.getBlob();
    var folder = DriveApp.getFolderById('THjgj698979XXXXXXXX');
     var docfile = Drive.Files.insert({
    title: "temp",
    mimeType: "application/vnd.google-apps.document",
  }).getId(); 

  var blobImage = DocumentApp.openById(docfile).insertImage(0, fileBlob);

  blobImage.setWidth(10);
  blobImage.setHeight(10);

   var fileBlob2 = blobImage.getBlob();  
  fileBlob2.setName(newFilename); 

  var result = folder.createFile(fileBlob2);

The image is scaled down from 4000x6000 to 2500x1667. Not to 10x10 :(

Do you see how to fix this problem ?

Thanks !

toto_tata
  • 14,526
  • 27
  • 108
  • 198

4 Answers4

8
  • You want to resize a downloaded image using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Issue and workaround:

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.

  1. Create the downloaded image as a file to Google Drive.
  2. Retrieve the file metadata from the created file.
    • Here, thumbnailLink is retrieved.
  3. Modify the width of the created image file by modifying thumbnailLink.
    • The value of thumbnailLink is like https://lh3.googleusercontent.com/###=s220.
    • When =s220 is changed, the size of thumbnail is also changed. This workaround uses this.
  4. Retrieve the blob from the modified link.
  5. Create the blob as a image file.
  6. Delete the temporal file.

Sample script:

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);
}
  • When you run the script, in above script, an image with 10 pixels of width is created. The height automatically follows to the aspect ratio of the image.
  • In this case, the mimeType of output image is image/png.

Note:

  • In this workaround, unfortunately, the resized image cannot be directly created from the blob. If you want to achieve this, for example, how about using an external API like PNG to PNG API of Convert API?
  • There is a GAS library including above script. This is also mentioned by Cooper's comment.

References:

If this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
1

used https://github.com/tanaikech/ImgApp with success. Some extra permission to presentation required though.

stolen from README

var res = ImgApp.doResize(fileId, width);
var width = res.resizedwidth;
var height = res.resizedheight;
DriveApp.createFile(res.blob.setName("filename"));
camous
  • 998
  • 11
  • 27
0

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.

0

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.

yuri
  • 3,182
  • 2
  • 16
  • 26