'How to make an image smaller while leaving the it in it's original proportions?
I'm trying to add a list of images from my Google drive to a Google doc using Google apps script. I made a list named images_list of links to the images that are in my Drive. I was able to add all the content I need to the doc and the images at the end of it.
Currently the images are huge in their size, and I turned it into 400X400. The problem is that not all them are square.
How can I make the image smaller while leaving the image in it's original proportions?
This is the code I'm using:
if(images_list[0]){
for(var i=0; i<images_list.length; i++){
try{
var imageID = images_list[i].slice(32,65);
var blob = DriveApp.getFileById(imageID).getBlob();
var imgDoc = body.appendImage(blob);
imgDoc.setWidth(400).setHeight(400)
bullet_index = bullet_index+1
}catch(e){}
}
}
Thanks!
Solution 1:[1]
For example it could be something like this:
if (images_list[0]) {
for (var i = 0; i < images_list.length; i++) {
try {
var imageID = images_list[i].slice(32, 65);
var blob = DriveApp.getFileById(imageID).getBlob();
var max = 400;
var w = imgDoc.getWidth();
var h = imgDoc.getHeight();
if (w > h) { h = h * (max / w); w = max }
else { w = w * (max / h); h = max }
imgDoc.setWidth(w).setHeight(h);
bullet_index = bullet_index + 1
} catch (e) { }
}
}
It will fit any image into a square 400x400 px while keeping the aspect ratio.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
