Contentful logo

Contentful Community

assetImageDimensions: specify aspect ratio for the images

Hi everyone!

How can I specify the validation of the aspect ratio for the images?
There are some validations out of the box like width, height, and size, but we need to specify the aspect ratio of the images too. So how can I specify it?

For instance, I can setup the widht, height and size validations but I need the aspect ratio validation too:
myContentType
.createField(‘image’)
.name(‘Image’)
.type(‘Link’)
.linkType(‘Asset’)
.localized(false)
.required(true)
.disabled(false)
.omitted(false)
.validations([

{ ‘linkMimetypeGroup’: [ ‘image’ ] }

,
{
“assetImageDimensions”: {
“width”:

{ “min”: 100, “max”: 1400 }

,
“height”:

{ “min”: 200, “max”: 1400 }

}
},
{
“assetFileSize”:

{ “min”: 0, “max”: 5000 }

}
])
}

Image dimensions

"Validates that the dimensions of an image are within a specified range. The entered values are interpreted as pixels and are inclusive of the range. Alternatively, you can specify only a desired height or width.

Please note that the validation will only be applied to assets that are identified as images."

Regards!

2 Likes

Hi @antonio.martinmoreno!

There isn’t a way to specify an aspect ratio validation unfortunately. That just isn’t an option for asset validations. If this is an important aspect of your content workflow, you could set up a webhook listener to check updated assets for a certain aspect ratio and only publish ones that meet your requirements or flag/unpublish/notify someone of ones that don’t meet requirements.

I’ll submit this as product feedback though and perhaps it’ll be added in the future.

Thanks for the suggestion!

Hello, I have the same requirement as we want to set up fav icon through contentful and we need a way to validate image based on a specific aspect ratio because I want contentful user to only be able to upload 16x16, 32x32, etc. could you help me to achieve this? I don’t want to set a range because a user could submit a non valid fav icon size.

1 Like

This should be a feature, especially since it looks like it is probably dead simple to code. I guess the devs just didn’t think of putting it in.