我有一张图片,我想调整它的大小。
应用程序脚本代码:
var fileId = 'idImage';
var img = DriveApp.getFileById(fileId).getBlob().;
newFile.getBody().insertImage(0, img);
对象 Blob 无法调整大小,那么如何调整图像大小?
Regards
我已经尝试过你提供的解决方案,但没有成功,我总是得到一个奇怪的图像比例。
我在这找到了一个可行的解决方案可怕的帖子(我是新手):
相当准确的代码:
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)
要做的是将图像作为 blob 插入(像往常一样),然后获取其尺寸,在文档中计算比率,最后设置其尺寸。
希望能帮助到你 !
顺便说一句,谢谢@serge-insas!
编辑:这是上述代码的更新版本,还可以修复图片的高度。对于专家来说可能会有一些奇怪的东西,但正如我所说,我是一个菜鸟!
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)