8000 [Validator] Support SVGs when validating image dimensions by JabriAbdelilah · Pull Request #45486 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Support SVGs when validating image dimensions #45486

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 priva 8000 cy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

JabriAbdelilah
Copy link
Contributor
Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets #45269
License MIT
Doc PR

Add SVG support in Image Validator

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.1 branch.

Review the GitHub status checks of your pull request 8000 and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

Hey!

I think @jschaedl has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

Copy link
Member
@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR. For completeness, can we add SVG tests for detecting portrait/landscape as well?

Comment on lines 242 to 247
/**
* @param mixed $value
*
* @return bool
*/
private function isSvg($value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use native type declarations. Can we be more precise about the $value type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$value can be an instance of File or string. that's way I put mixed

upadte validator changelog

Use Mime Component, Check for corrupted XML
@JabriAbdelilah
Copy link
Contributor Author

@nicolas-grekas @derrabus can you re-check, please?

$width = intval($svgAttributes->width);
$height = intval($svgAttributes->height);

return [$width, $height];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's common for others, but I usually work with SVG files and the following is not that uncommon:

  • To not have any width or height attribute and just rely on the viewBox attribute dimensions
  • To have values like 100% in the width or height attributes because those images are embedded in other parent elements with some height/width defined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @javiereguiluz here. Does it mean that this is not that useful? The size of an SVG is not that important anyway, maybe just the ratio? cc @symfony/mergers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just the ratio?

I agree this one would be important.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, I would only implement the ratio validation for SVGs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine to also validate width and height if we make sure to skip this validation in case the given width or height are relative values (like mentioned by Javier).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I fail to see how this is useful. A 100x200 SVG file looks perfectly fine at 20x40 or 2000x4000.

@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@fabpot
Copy link
Member
fabpot commented Aug 29, 2022

I'm going to close here for the reasons explained in the code review and because there is no more feedback here.
Feel free to reopen with only the ratio checks.

@fabpot fabpot closed this Aug 29, 2022
fabpot added a commit that referenced this pull request Dec 29, 2024
…imecolin)

This PR was merged into the 7.3 branch.

Discussion
----------

[Validator] Validate SVG ratio in Image validator

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        | Fix #45269
| License       | MIT

Implement ratio check for SVG images. Checking SGV size is not relevant as a SVG image can be enlarged without loss, but ratio can be important to check.

Currently, the validator add a violation with `SIZE_NOT_DETECTED_ERROR` in case of SVG image.

SVG size is guessed from viewbox, width and height attributes. Viewbox will provides default size, width and height can override viewbox size if they are number. Width and height as percentage are ignored as the final size will depend on the container.

I use `preg_match` instead of `\DomDocument` or `simplexml` functions to extract viewBox, width and height in order to avoid new dependencies on `ext-dom` or `ext-simplexml`.

In case of SVG, `minWidth`, `maxWidth`, `minHeight`, `maxHeight`, `minPixels` and `maxPixels` are ignored because not relevant. Only `maxRatio`, `minRatio`, `allowSquare`, `allowLandscape` and `allowPortrait` can generate violations, like suggested in the comments of the abandoned #45486.

Commits
-------

c4ad092 [Validator] Validate SVG ratio in Image validator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0