Google脚本:如何调整Blob中的图片大小? [英] Google script : how to resize image in blob?

查看:207
本文介绍了Google脚本:如何调整Blob中的图片大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Blob,其中包含一个在Google云端硬盘中远程加载和创建的图像:

I have a blob containing an image that is remotely loaded and created in Google Drive:

var response = UrlFetchApp.fetch(fileURL);
var fileBlob = response.getBlob();
var folder = DriveApp.getFolderById('THjgj698979XXXXXXXX');
var result = folder.createFile(fileBlob);

我想要调整此fileBlob中包含的图像的大小.我该怎么办?

What I want is to resize the image contained in this fileBlob. How can I do that?

为您提供信息,一篇可以提供帮助但最终并没有真​​正帮助我的帖子:(说明如何调整图像大小,而不是如何调整Blob中的图像大小) 在Google Apps脚本中调整图像大小

For your information, a post that can help but finally didn't really help me:(explains how to resize an image, not how to resize an image in a blob) Resizing image in Google Apps Script

谢谢

这是我现在要做的.但是我有一个问题:图像按比例缩小,但没有达到我指定的大小...

Here is what I do now. But I have got a problem : the image is scaled down but not at the size I specify...

 var response = UrlFetchApp.fetch(fileURL);
    var fileBlob = response.getBlob();
    var folder = DriveApp.getFolderById('THjgj698979XXXXXXXX');
     var docfile = Drive.Files.insert({
    title: "temp",
    mimeType: "application/vnd.google-apps.document",
  }).getId(); 

  var blobImage = DocumentApp.openById(docfile).insertImage(0, fileBlob);

  blobImage.setWidth(10);
  blobImage.setHeight(10);

   var fileBlob2 = blobImage.getBlob();  
  fileBlob2.setName(newFilename); 

  var result = folder.createFile(fileBlob2);

图像从4000x6000缩小到2500x1667.不能设为10x10:(

The image is scaled down from 4000x6000 to 2500x1667. Not to 10x10 :(

您看到如何解决此问题了吗?

Do you see how to fix this problem ?

谢谢!

推荐答案

  • 您要使用Google Apps脚本调整下载图像的大小.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    不幸的是,在当前阶段,尚无方法直接在Google Apps脚本中调整图像的大小.但是有解决方法.此解决方法的流程如下.

    Unfortunately, in the current stage, there are no methods for resizing directly the image in Google Apps Script. But there is a workaround. The flow of this workaround is as follows.

    1. 将下载的图像创建为文件添加到Google云端硬盘.
    2. 从创建的文件中检索文件元数据.
      • 在这里,检索到thumbnailLink.
    1. Create the downloaded image as a file to Google Drive.
    2. Retrieve the file metadata from the created file.
      • Here, thumbnailLink is retrieved.
    • thumbnailLink的值类似于https://lh3.googleusercontent.com/###=s220.
    • 更改=s220时,缩略图的大小也会更改.此解决方法使用此方法.
    • The value of thumbnailLink is like https://lh3.googleusercontent.com/###=s220.
    • When =s220 is changed, the size of thumbnail is also changed. This workaround uses this.

    示例脚本:

    在运行脚本之前,请设置变量widthoutputFilenameurl.另外,请在高级Google服务中启用Drive API.

    Sample script:

    Before you run the script, please set the variables of width, outputFilename and url. And also, please enable Drive API at Advanced Google services.

    function myFunction() {
      var width = 10; // Please set the size of width with the unit of pixels.
      var outputFilename = "sample.png"; // Please set the output filename.
      var url = "###";
    
      var blob1 = UrlFetchApp.fetch(url).getBlob().setName("sampleImage_temporal");
      var fileId = DriveApp.createFile(blob1).getId();
      var link = Drive.Files.get(fileId).thumbnailLink.replace(/\=s.+/, "=s" + width);
      var blob2 = UrlFetchApp.fetch(link).getBlob().setName(outputFilename);
      var file = DriveApp.createFile(blob2);
      Drive.Files.remove(fileId);
    }
    

    • 运行脚本时,在上面的脚本中,将创建一个宽度为10像素的图像.高度自动跟随图像的长宽比.
    • 在这种情况下,输出图像的mimeType为image/png.
      • When you run the script, in above script, an image with 10 pixels of width is created. The height automatically follows to the aspect ratio of the image.
      • In this case, the mimeType of output image is image/png.
        • 不幸的是,在此替代方法中,无法直接从Blob中创建调整大小的图像.例如,如果要实现此目标,如何使用外部API,例如 PNG到PNG API转换API ?
        • 有一个一个GAS库,其中包括上述脚本. 库珀的评论也提到了这一点.. li>
        • In this workaround, unfortunately, the resized image cannot be directly created from the blob. If you want to achieve this, for example, how about using an external API like PNG to PNG API of Convert API?
        • There is a GAS library including above script. This is also mentioned by Cooper's comment.
        • Advanced Google services
        • Files: get of Drive API
        • ImgApp of GAS library

        如果这不是您想要的方向,我深表歉意.

        If this was not the direction you want, I apologize.

        这篇关于Google脚本:如何调整Blob中的图片大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆