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
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
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.
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
newFile
would presumably be defined earlier in the script-- it's not a complete example. –
Paltry 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);
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);
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.
© 2022 - 2024 — McMap. All rights reserved.