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

Dustin Michels
  • 2,951
  • 2
  • 19
  • 31
Hann
  • 727
  • 3
  • 11
  • 22

4 Answers4

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

rogowskibart
  • 85
  • 1
  • 2
  • 8
Hann
  • 727
  • 3
  • 11
  • 22
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);
Community
  • 1
  • 1
Romain
  • 125
  • 1
  • 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. – Serge insas Feb 16 '16 at 16:01
  • 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-insas – Romain Feb 16 '16 at 17:56
  • @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); } – Romain Feb 16 '16 at 18:00
  • '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 ;) – Serge insas Feb 16 '16 at 22:46
  • @Sergeinsas there you are : https://docs.google.com/spreadsheets/d/1-UgEA-W-J9pwCzviA7z2xzB-FFA1KsAg0SUBdv8RMko/edit?usp=sharing It's a spreadsheet I want to create for foreign students, in order to create some vocabulary with images. EDIT : wrong link, corrected now. – Romain Feb 17 '16 at 09:49
  • I don't see any problem... what am I missing ? – Serge insas Feb 17 '16 at 17:25
  • There is always a chariot return inserted as well as the image. Illustration : https://drive.google.com/file/d/0B5ymfuxKy3Oyekk1MHU3TU1rRHM/view?usp=sharing Thanks for having a look ! – Romain Feb 18 '16 at 07:49
  • 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 ! thx – Serge insas Feb 18 '16 at 08:20
  • ligne 89 dans le script : var cellImage = ligne.appendTableCell().insertImage(0, image); – Serge insas Feb 18 '16 at 09:07
  • 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. – Romain Feb 18 '16 at 16:44
  • Hahaha...I didn't notice that...OK, I'll continue to dig into it ;) – Serge insas Feb 18 '16 at 16:47
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);
Dustin Michels
  • 2,951
  • 2
  • 19
  • 31
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.