-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add min/max amount of pixels to Image constraint #22288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Moved from 3.3.0 to 3.4.0
@@ -25,6 +25,8 @@ class Image extends File | |||
const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a'; | |||
const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645'; | |||
const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c'; | |||
const TOO_FEW_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TOO_FEW_PX_ERROR
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That probably is a bit better, indeed.
@@ -72,6 +78,8 @@ class Image extends File | |||
public $minWidthMessage = 'The image width is too small ({{ width }}px). Minimum width expected is {{ min_width }}px.'; | |||
public $maxHeightMessage = 'The image height is too big ({{ height }}px). Allowed maximum height is {{ max_height }}px.'; | |||
public $minHeightMessage = 'The image height is too small ({{ height }}px). Minimum height expected is {{ min_height }}px.'; | |||
public $minPixelsMessage = 'The image has to few pixels ({{ pixels }} pixels). Minimum amount expected is {{ min_pixels }} pixels.'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will show an arbitrary number of pixels ({{ pixels }}
), perhaps {{ width }} × {{ height }}
is more user friendly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the error message should directly show the problem, so at least show the direct comparison between the set limit and the problematic value. If it helps, maybe there should be some kind of calculation shown. This will make the message more bloated, however. What can be done is that al least the height and width parameters will be set, so custom messages can use these.
If someone has an idea about this, please let us know :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What can be done is that al least the height and width parameters will be set, so custom messages can use these.
Makes sense 👍 no strong opinion about the default message (just proposed an alternative). Personally i'd favor 300 × 250
vs. 75000 pixels
Just a curious question: what is the use case? |
This would constraint an image based on square units, whatever the width/heights may be. One could prefer such a constraint over e.g. filesize based. |
Ok, I am just thinking of a valid usage of just this one constraint. eg. I would think that |
…ssage, fixed bad english
Fair question! I havent one :) I can only assume one may want to restrict based on pixels (vs. dimensions or filesize). So i support the feature for the sake of completion solely. |
Fair question indeed. I hope this answer makes it more clear. I'm using this for two things: the first is an image wall where images may have a maximum area, but I don't care about their shape. The second is that processing images takes memory that scales with the area, so again I don't care about the shape, but I do about the area. So why can't we restrict based on the currently available constraints? It's all about area. Consider Table 1, with an examples of a tiny image with a max area of 6 pixels squared. Based on this we can create a constraint set given by Table 2. Now consider an image of 3x3 pixels with ratio 1.00 and area 9 px2. It falls well within the currently available constraint range, but is not valid due to it's oversized area. Beging able to validate the area helps to overcome this shortage. In real world applications, of course, an image width of for example 1 pixel is not very useful most of the time. Therefore a combination of minWidth and minHeight or ratio constraints is a good idea to do.
Table 1: example values for an image of at most 6 px2.
Table 2: currently available constraint set. |
@akeeman thanks for your explanation! Validating at the approximate cost of processing the image is a perfect use case. 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change the base to 3.4 instead of master and rebase on current 3.4? Thanks.
@@ -25,6 +25,8 @@ class Image extends File | |||
const TOO_NARROW_ERROR = '9afbd561-4f90-4a27-be62-1780fc43604a'; | |||
const TOO_HIGH_ERROR = '7efae81c-4877-47ba-aa65-d01ccb0d4645'; | |||
const TOO_LOW_ERROR = 'aef0cb6a-c07f-4894-bc08-1781420d7b4c'; | |||
const TOO_FEW_PX_ERROR = '1b06b97d-ae48-474e-978f-038a74854c43'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename to TOO_FEW_PIXEL_ERROR
? Same for the other constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
I've cleaned up my forks a while ago, deleting the branch associated with this PR. I've found no way of restoring or re-linking a branch, leaving me unable to edit this PR. Therefore I opened a new one, succeeding his one. Please check #23431. |
…traint (akeeman) This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Add min/max amount of pixels to Image constraint | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#7756 Useful for asserting image sizes/areas in pixels, for instance to estimate processing work load. **This PR continues #22288**. I cleaned up my associated branch a while ago and found no way of restoring or re-linking. @fabpot: "Could you rename to [TOO_FEW_PIXEL_ERROR](https://github.com/akeeman/symfony/blob/9ab5263d712881f8aa24920631685d14acd3a19b/src/Symfony/Component/Validator/Constraints/Image.php#L28)? Same for the other constant?" This is done. @fabpot "Can you change the base to 3.4 instead of master and rebase on current 3.4? Thanks." This is done too. Commits ------- 9ab5263 add minimum and maximum amount of pixels to Image validator
…raint (akeeman) This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #7756). Discussion ---------- [Validator] Add min/max amount of pixels to Image constraint Documentation for symfony/symfony#22288 Commits ------- 32f90ee Add minPixels and maxPixels constraints
Useful for asserting image sizes/areas in pixels.