Resizing image in Google Apps Script

前端 未结 4 626
礼貌的吻别
礼貌的吻别 2021-01-01 00:49

I have an image and I want to resize this.

App Script code:

var fileId = \'idImage\';
var img = DriveApp.getFileById(fileId).getBlob().;
newFile.getB         


        
相关标签:
4条回答
  • 2021-01-01 01:27

    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);
    
    0 讨论(0)
  • 2021-01-01 01:32

    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);
    
    0 讨论(0)
  • 2021-01-01 01:33

    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

    0 讨论(0)
  • 2021-01-01 01:36

    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.

    0 讨论(0)
提交回复
热议问题