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