0

so I have been making some image requests with the google docs api using javascript, but I have been unable to change the image size when I make the request.

All the images seem to be appearing in their original size in the google doc.

So I am wondering what I am doing wrong. The documentation seems to be pretty clear:

https://developers.google.com/docs/api/reference/rest/v1/documents/request#InsertInlineImageRequest

Basically it just says to specify objectSize width and height, using magnitude and units (PT).

Here is the function I use to append to my full request, where I specify all of these.

function imageRequest (url: string, width: number, height: number) {
  const request:Array<object> = [ {
    insertInlineImage: {
      uri: url.toString(),
      objectSize: {
        height: {
          magnitude: height,
          unit: 'PT'
        },
        width: {
          magnitude: width,
          unit: 'PT'
        }
      },
      location: {
        index: 1
      }      
    },
  } ];
  return request;
}

And then I'm pushing each image request to the full request like.

request.push(imageRequest(image.image.url, 468, 648));

but whatever I put for the width and height is not doing anything. The images are always the original size.

The images are public on an s3 style hosting at Wasabi (similar to amazon s3)

I cannot think of what the problem could be, maybe it's something small that I am overlooking?

help is appreciated, thanks.

9
  • Have you taken into account the unit conversion? 1 PT is larger than 1 PX, so an image of 468x648 PT would be equivalent to a 624x864 PX image. It could be that you're making them larger than you're expecting. Do you have a sample image? Commented May 23, 2022 at 21:39
  • @Daniel - it seems to be that any value I enter doesn't have any effect whatsoever so I'm not sure that's it. I tried setting all images in my export to 200 * 200 PT for example and the images don't change size or proportion at all. Commented May 23, 2022 at 22:14
  • Can I ask you about the image size of the original image? Commented May 23, 2022 at 23:38
  • @Tanaike - the same is happening for many sizes, some smaller and some larger. Commented May 24, 2022 at 0:08
  • Thank you for replying. For example, when both width and height are set, it seems that the smaller value is used for keeping the aspect ratio. By considering this situation, when I tested your request body, the inserted image can be resized. So, I cannot replicate your situation. Can you provide the sample image you want to use? By this, I would like to test it. Commented May 24, 2022 at 0:20

1 Answer 1

1

The image dimensions are defined by the objectSize property and the Docs documentation states a few rules that define the final size of the image:

If neither width nor height is specified, then a default size of the image is calculated based on its resolution.

If one dimension is specified then the other dimension is calculated to preserve the aspect ratio of the image.

If both width and height are specified, the image is scaled to fit within the provided dimensions while maintaining its aspect ratio.

From these rules it seems that Docs will prioritize the aspect ratio of an image over the exact size, so even if you specify a certain set of dimensions, Docs may resize it to preserve the ratio. As a workaround you can specify only the dimension that you deem more important, (width or height) and Docs will calculate the other one automatically.

In addition, it seems that Docs uses points (PT) rather than pixels (PX) as units to measure dimensions. One point is 1/72 inch while one pixel is 1/96 inch, so if you're used to calculating sizes in pixels as it's most common you'll need to make this conversion as well. A pixel will be 0.75pt while a point will be 1.333...px.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.