-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add support for SVG files to image validation constraint #15460
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
Comments
The problem seems to lie in the current Simple test script: use Symfony\Component\HttpFoundation\File\File;
$svg = new File("example.svg");
var_dump($svg->getMimeType()); // string 'image/svg+xml' (length=13)
// example_after_svgo.svg is a minified SVG file, created with the following command
//
// $ svgo --output=example_after_svgo.svg example.svg
//
// https://www.npmjs.com/package/svgo
$svgMin = new File("example_after_svgo.svg");
var_dump($svgMin->getMimeType()); // string 'text/plain' (length=10) I think a way to solve this is to register another guesser, a specialized That way both the form would work correctly and the mimetype guessing would be improved globally. |
@cH40z-Lord Just to be sure, is it a typo in your PR description or Google Chrome send you images/svg+xml? (note the "s") |
@yguedidi sorry, it is indeed a typo. |
@yguedidi @cH40z-Lord right, but keep in mind that the mimetype as provided from the browser is discarded and instead the mimetype is fetched from the actual uploaded file. This is necessary as the mimetype of the request can be altered to be anything and doesn't have to be connected to the file itself. |
@xabbuh as I read it, you should add the |
I've started to implement a first rough prototype that addresses the issues discussed here. Right now I've got a working Now I wanted to have a look at the Image constraint and think about how to integrate SVG images (and possibly other vector formats for the future) into it. My proposed changes would look like this: 1)
|
The reported issue is legit in my opinion. But the proposed solutions look very complex. As @apfelbox said, the problem only occurs when the SVG is minimized with // example_after_svgo.svg is a minified SVG file, created with the following command
//
// $ svgo --output=example_after_svgo.svg example.svg
//
// https://www.npmjs.com/package/svgo
$svgMin = new File("example_after_svgo.svg");
var_dump($svgMin->getMimeType()); // string 'text/plain' (length=10) But if we are talking about minimizing and not compressing, how can the MIME type guesser fail? The file should still be a valid SVG file. |
Well the proposed |
Just having a related issue with 3.3: $uploadedFile->guessExtension() on an uploaded svg file returns ".txt" |
Hello, is there any plan to fix this ? |
The guessed MIME type for SVG files is likely to be wrong out of the box. I just got |
How to fix this? |
Here's my workaround: https://gist.github.com/teohhanhui/90bbc4a61888ad2e92a160d8459db300 |
This PR was squashed before being merged into the 4.3-dev branch (closes #29896). Discussion ---------- [Mime] Add the component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #28832 #21985 makes #15460 trivial | License | MIT | Doc PR | symfony/symfony-docs#10886 This has been on my todo-list for X years :) Commits ------- bdca5d9 tweaked code 5268389 [Mime] added freedesktop as a source for mime types 74ca91d [Mime] added the component d7ee0ec [HttpFoundation] updated File code
I've run the script shown in #15460 (comment) and with the latest Symfony 4.3 version and Mime component, I get the expected results. I used this SVG image: https://symfony.com/logos/symfony_black_02.svg First, "as is" and second, after running The first lines of the original logo are: <?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="289.333px" height="122.833px" viewBox="0 0 289.333 122.833" enable-background="new 0 0 289.333 122.833"
xml:space="preserve"> The first line of the "compressed" (minified) logo is: <svg xmlns="http://www.w3.org/2000/svg" width="289.333" height="122.833"> The results of running the script are:
So, this no longer returns |
@javiereguiluz When was this fixed? In which PR? EDIT: #29936 |
Was this fixed? My svg is being identified as image/png. |
With the rise of SVG images all over the web I think it's pretty fair to say that having support for the already existing Image validator constraint would benefit everyone.
Let's have a look at the following scenario where we'd like to give the opportunity to someone to upload either a GIF, JPEG, PNG or an SVG.
The MIME type Google Chrome is sending matches images/svg+xml and still I'm getting the following error message: "This file is not a valid image."
A solution to this problem would be to write a custom validation constraint. However, I'm pretty sure that this is one of the things that might be needed more often in the future so the best place for it is inside the Symfony Core.
Opinions?
The text was updated successfully, but these errors were encountered: