Asset's width and height are returned as strings instead of integers
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
Description
According to the API reference, an image asset's width and height should be returned as integer, float or null. However, Asset::getWidth() and Asset::getHeight consequently return numeric strings, which makes it annoying to do math with them.
Steps to reproduce
I'm not sure if this is caused by my setup, I haven't done anything special. I just created an image volume, an assets field and tried to access the image's width/height in a Twig template. Everywhere I do that the methods consequently return strings.
I looked into the craft\elements\Asset class. Asset::getWidth() and Asset::setWidth() (as well as the corresponding height methods) have docblocks indicating the type as int|float|null, but don't use typehints and don't enforce the type. I'm not sure where the width and height are actually set, but apparently it's setting them as strings and the class doesn't cast them to integer.
We're using PHP 8 with GD (since Imagick isn't supported yet), maybe it's related to that?
Additional info
- Craft version: 3.6.12
- PHP version: 8.0.3
- Image driver & version: GD 8.0.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting the craft\elements\Asset class, especially Asset::getWidth(), Asset::setWidth(), and the corresponding height methods. Reproduce the issue by accessing an image asset's dimensions in a Twig template, then trace where those values are assigned. Done means width and height are returned with the documented numeric or null types rather than numeric strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100