谷歌应用程序脚本驱动应用程序文件:确定图像是横向还是纵向



我正在编写一个Google Docs插件。它的作用:它从谷歌硬盘文件夹中取出图像,并将其放入谷歌文档中。……简而言之,这就是它的作用;-)

我想知道在将图像插入谷歌文档之前,是否有办法找到图像方向(横向/纵向)。(一旦插入,这不是问题,但我需要在插入图像之前弄清楚,这样脚本运行得更快。)

所以我需要的是这样的东西:

function getImagesFromFolder(ID){
DriveApp.getFolderById(ID).getFiles();
var mimetypes = ["image/png","image/jpeg","image/gif","image/bmp"];
var images = [];
while (files.hasNext())
{
 var file = files.next();
 if( mimetypes.indexOf(file.getMimeType()) != -1) 
  {
   var imageObject = {};
   imageObject.fileAsJpeg = file.getBlob().getAs("image/jpeg")
   imageObject.orientation = getImagefileOrientation(file) 
//Here is the issue! I need a 'getImageFileOrientation-function' that returns 'landscape' or 'portrait'
   images.push(imageObject); 
  }
}
return images;
}

 //------Later on I will be using----------
    for (var i = 0;i<images.length;i++)
    {
     if (images[i].orientation == 'portrait')
      {
      // some code that rotates the image when it is inserted in the Google Doc. Not the problem.
      }
      else
      {
       googleDocBody.insertImage(images[i].fileAsJpeg); //Not the problem
      }
    }

提前感谢!

Jasper

从技术上讲,答案是可以的,但对于so来说,这样做的实际代码太长了,因为Blob中这样做的方法因图像类型而异。

大多数图像的位代码头存储尺寸,从那里可以简单地通过数学问题来确定方面。记录这一点的标头是不同类型的标头。

如果将它们插入到一个空的伪文档中并使用getWidthgetHeight对您来说不够快,那么使用UrlFetch到第三方API我们也会很慢。

在这种情况下,我建议将图像大小(node.js库)移植到您的Apps脚本中,或者将其作为一个单独的库。节点库包括用于处理流中图像的函数,但您可以在市中心进行钻探,只需包装detectcalculate方法。

最新更新