Resizing image in Google Apps Script

前端 未结 4 686
礼貌的吻别
礼貌的吻别 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);
    

提交回复
热议问题