在向Google Docs API请求之前,压缩图像(来自公共URI)

发布于 2025-02-01 01:44:08 字数 830 浏览 3 评论 0原文

我正在尝试从另一个源文档编程构建Google Doc,并且该源文档包含在S3服务器上托管的图片。

因此,基本上,我正在从源文档中获取图像的URI,然后直接与URI进行我的GDOC请求:

  const request = [{
    insertInlineImage: {
      uri: myUriString,
      location: {
        index: 1
      }      
    },
  }];

我的问题是,有时我会从Google Docs服务器中收回此错误:'无效请求:提供的图像太大了。

我在文档必须小于50MB,不能超过25百万像素,并且必须以PNG,JPEG或GIF格式之一。”

因此,基本上,我需要将其复制到Google文档的S3上的一些图像太大而无法直接提出请求。

问题是我不太了解最好的方法(或者甚至可能的话)压缩从S3获得URI和提出GDOC请求之间的图像(因为我只是真正获得URI,而不是Blob

)我需要做一些事情,例如将图像向下拉,压缩图像,然后再次将压缩图像保存到S3,然后将新创建的URL用于GDOCS请求?

这似乎是关于要做的事情,所以我只是想知道是否有人有更好的解决方案。

我的直觉是,我缺少或不了解某些东西。

提前致谢。

I'm trying to build a google doc programmatically from another source doc, and the source document contains pictures that are hosted on an s3 server.

So basically I'm getting the uri for the images from the source doc, then making my gdocs request directly with the uri like this:

  const request = [{
    insertInlineImage: {
      uri: myUriString,
      location: {
        index: 1
      }      
    },
  }];

My problem is that sometimes I'm getting this error back from the google docs server: 'Invalid request: The provided image is too large.'

I found in the documentation that "Images must be less than 50MB in size, cannot exceed 25 megapixels, and must be in one of PNG, JPEG, or GIF format."

So basically some of the images hosted on S3 that I need to copy to google docs are too large to make the request directly.

The problem is I don't really understand the best way (or if it's even possible) to compress the image between getting the uri from s3 and making the gdocs request (since I'm only really getting the uri, not the blob)

Maybe I need to do something like pull the image down, compress it, then save the compressed image again to s3, and then use the newly created url for the gdocs request?

That seems like a very round about thing to do, so I'm just wondering if anyone has a better solution.

My gut feeling is that there is something I'm missing or not understanding.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2025-02-08 01:44:08

为了减少图像大小和图像数据大小,以下流程如何?该流程用于使用Google API降低图像大小和图像数据大小。

流:

  1. 从URL下载图像。
  2. 使用Drive API将下载的图像数据上传到Google Drive。
  3. 使用驱动API检索Thumbnaillink。
  4. 修改缩略图的图像大小如下。
    • thumbnaillink就像https://lh6.googleusercontent.com/### #k = s220
    • 请将其修改为https://lh6.googleusercontent.com/####k = s1000= S220被修改为= S1000
    • 这样,缩略图的宽度变为1,000像素。
  5. 使用修改后的缩略图链接使用DOCS API插入图像。

参考:

In order to reduce the image size and image data size, how about the following flow? This flow is used for reducing the image size and image data size using Google API.

Flow:

  1. Download the image from the URL.
  2. Upload the downloaded image data to Google Drive using Drive API.
  3. Retrieve the thumbnailLink using Drive API.
  4. Modify the image size of the thumbnail as follows.
    • The thumbnailLink is like https://lh6.googleusercontent.com/###k=s220.
    • Please modify this to https://lh6.googleusercontent.com/###k=s1000. =s220 is modified to =s1000.
    • By this, the width of the thumbnail becomes 1,000 pixels.
  5. Insert the image using the modified thumbnail link using Docs API.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文