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

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

问题描述

我有一个包含远程加载并在 Google 云端硬盘中创建的图像的 blob:

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 Script 中没有直接调整图像大小的方法.但是有一种解决方法.此变通方法的流程如下.

    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.
    • 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.

    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.
      • 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.

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

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

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

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