2

I get an image on a remote server with :

  var response = UrlFetchApp.fetch(fileURL);
  var fileBlob = response.getBlob();

How to get the image from the blob ? I would like to use this image in order to get its size (with getHeight() and getWidth()) and do different other operations on it.

Thanks

toto_tata
  • 14,526
  • 27
  • 108
  • 198

1 Answers1

5
  • 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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for your answer. How should I do if I want to resize the image and then place it in Google Drive. See my question here: https://stackoverflow.com/questions/58958716/google-script-how-to-resize-image-in-blob Thanks ! – toto_tata Nov 21 '19 at 11:01
  • @Regis_AG Thank you for replying. Before I think of your another question, I would like to resolve your this question. How about this? If my answer was not useful for this question, could you please tell me it? I would like to modify it. – Tanaike Nov 21 '19 at 11:33
  • Hi @Tanaike Unfortunately, I didn't manage to do what I want (resizing an image) thanks to your answer. I want to resize it either : 1) once obtained from server before creating it on Google Drive, or 2) Once created on Google Drive. Do you have any idea about how to do this? Thanks !!! – toto_tata Nov 21 '19 at 13:34
  • See the EDIT I added to my question here : https://stackoverflow.com/questions/58958716/google-script-how-to-resize-image-in-blob Do you see a way to resize my image ? – toto_tata Nov 21 '19 at 14:18
  • @Regis_AG Thank you for replying. I have to apologize for my poor English skill. In [your this question](https://stackoverflow.com/q/58959206), you want to retrieve the height and width of the image from the blob. I understood like this. About the resize of the image, you have already been posted it at [here](https://stackoverflow.com/questions/58958716/google-script-how-to-resize-image-in-blob). So at first, I would like to resolve about your this question for retrieving the height and width of image. If my answer doesn't resolve your this question, please tell me. I would like to modify it. – Tanaike Nov 21 '19 at 22:08
  • Hi @Tanaike Indeed, you answered well to the present question. When I asked this question (on how to get image from blob), I thought that it was enough to get its dimensions and resize it. So yes, I can get the dimensions but I still cannot resize it :( Reason why I asked another question specifically for this resizing issue here : https://stackoverflow.com/questions/58958716/google-script-how-to-resize-image-in-blob If you have any idea, let me know... – toto_tata Nov 21 '19 at 22:30
  • @Regis_AG Thank you for replying. I'm glad your question was resolved. About [your another question](https://stackoverflow.com/questions/58958716/google-script-how-to-resize-image-in-blob), I would like to think of it. If you give me a time to do, I'm glad. – Tanaike Nov 21 '19 at 22:32