Resizing image in Google Apps Script
Asked Answered
M

4

8

I have an image and I want to resize this.

App Script code:

var fileId = 'idImage';
var img = DriveApp.getFileById(fileId).getBlob().;
newFile.getBody().insertImage(0, img); 

Object Blob can't resize so how can I resize my image?

Regards

Milk answered 23/4, 2014 at 9:39 Comment(0)
V
0

Just as an FYI, you don't need to call getBlob().. anything that has a getBlob() can be passed in directly wherever a Blob is needed.

Vtehsta answered 23/4, 2014 at 9:46 Comment(1)
this isn't an answer?!Crowboot
M
4

I found a solution :

var fileId = 'ImageID';
var img = DriveApp.getFileById(fileId).getBlob();
var imgDoc = newFile.getBody().insertImage(0, img); 
imgDoc.setWidth(630);

It's an inlineImage object.

link : https://developers.google.com/apps-script/reference/document/inline-image

Milk answered 23/4, 2014 at 10:4 Comment(2)
This throws ReferenceError: "newFile" is not defined.Loudmouth
newFile would presumably be defined earlier in the script-- it's not a complete example.Paltry
R
4

I've tried the solution you gave, but no success, I always get a bizarre image ratio. I've found a working solution in this frightening post (I am a newbie):

Quite exact code :

var inlineI = GDoc.appendImage(img);
var width = inlineI.getWidth();
var newW = width;
var height = inlineI.getHeight();
var newH = height;
var ratio = width/height

if(width>640){
newW = 640;
newH = parseInt(newW/ratio);
}
inlineI.setWidth(newW).setHeight(newH)

The thing to do is to insert the image as blob (as usual), and THEN get its dimensions, within the doc to calculate the ratio, and FINALLY set its dimensions. Hope it helps !

btw, thanks @serge-insas !

EDIT : this is an updated version of the above code, to also fix the height of the picture. There might be some weird stuff for specialists, but as said, I'm a noob !

if(insertImage == true){
  var cellImage = ligne.appendTableCell().insertImage(0, image);
  //get the dimensions of the image AFTER having inserted it to fix
  //its dimensions afterwards
  var originW =  cellImage.getWidth();
  var originH = cellImage.getHeight();
  var newW = originW;
  var newH = originH;
  var ratio = originW/originH

  if(originW>maxWidth){
    newW = maxWidth;
    newH = parseInt(newW/ratio);
  }
  cellImage.setWidth(newW).setHeight(newH).setAttributes(styleImage);
  var newWW = cellImage.getWidth();
  var newHH = cellImage.getHeight();
  var newRatio = newHH/newWW;
  Logger.log("image width = "+newWW);
  Logger.log("image height = "+newHH);

  if(newHH>maxWidth){
    newHH = maxHeight;
    newWW = parseInt(newHH/newRatio);
  }
  cellImage.setWidth(newWW).setHeight(newHH);
  cellImage.getParent().setAttributes(styleImage);
Raby answered 16/2, 2016 at 15:50 Comment(11)
I love the "frightening" comment ;-) it is indeed a bit long and confusing... glad you found your way ... thanks also for mentioning the source.Flann
Reading your code, I guessed you speak French ? I do ! But... I still have a minor issue : when I insert an image, I get an undesired CHAR(10) (line return, or line break, dunno how they call it in English (un bon vieux retour à la ligne, quoi) in the cell. Any idea ? Can't pas my code here, not enough place. @serge-insasRaby
@Sergeinsas BTW, I've added a nice feature : it also resizes the height of the image, so that they don't modifiy the line height of the table. cellImage.setWidth(newW).setHeight(newH).setAttributes(styleImage); var newWW = cellImage.getWidth(); var newHH = cellImage.getHeight(); var newRatio = newHH/newWW; if(newHH>maxWidth){ newHH = maxHeight; newWW = parseInt(newHH/newRatio); }Raby
'line feed' or 'chariot return' pour le retour à la ligne ;-), d'où l'abréviation cr lf pour chr10+chr13... l'avantage d'avoir connu les vieux modem en RS232... could you share a dummy example of the issue you are having ? I just can't reproduce it. (preferably in a read only shared spreadsheet) thx, merci ;)Flann
@Sergeinsas there you are : docs.google.com/spreadsheets/d/… It's a spreadsheet I want to create for foreign students, in order to create some vocabulary with images. EDIT : wrong link, corrected now.Raby
I don't see any problem... what am I missing ?Flann
There is always a chariot return inserted as well as the image. Illustration : drive.google.com/file/d/0B5ymfuxKy3Oyekk1MHU3TU1rRHM/… Thanks for having a look !Raby
That's in the document version isn't it ? you shared the spreadsheet version which doesn't have that 'new line'. Now I've run the script and have the doc version ! thxFlann
ligne 89 dans le script : var cellImage = ligne.appendTableCell().insertImage(0, image);Flann
Thanks Serge ! I just understood the "childIndex" thing : it's the index of the content itself (character position), not the index of the cell... Ok, now that I've changed this 1 to a 0, the image is inserted before that cr, and there is still one after. In fact, the only thing I want in the cell is the image.Raby
Hahaha...I didn't notice that...OK, I'll continue to dig into it ;)Flann
P
4

Calling insertImage() returns an InlineImage object (or an OverGridImage if you're working with a spreadsheet.)

In either case, you could use a function like this to resize the image:

function resizeImg(img, targetHeight) {
    var height = img.getHeight();
    var width = img.getWidth();
    var factor = height / targetHeight;
    img.setHeight(height / factor);
    img.setWidth(width / factor);
};

This approach ensures the image gets scaled proportionally, to your target height (in pixels).

Example usage:

var myImg = newFile.getBody().insertImage(0, img); 
resizeImg(myImg, 60);
Paltry answered 20/11, 2018 at 18:8 Comment(0)
V
0

Just as an FYI, you don't need to call getBlob().. anything that has a getBlob() can be passed in directly wherever a Blob is needed.

Vtehsta answered 23/4, 2014 at 9:46 Comment(1)
this isn't an answer?!Crowboot

© 2022 - 2024 — McMap. All rights reserved.