9

How to Allowed file types svg image upload magento 2

Show in system.log

main.ERROR: Unsupported image format. File: /var/www/html/pub/media//logos_1.svg

1
+50

This module makes it possible to upload different filetypes inside the WYSIWYG-editor.

The following filetypes are available by default and it is possible to add extra filetypes to the allowed filetypes in the configuration of the module (General > Content Management > WYSIWYG Options > Extra Allowed Filetypes):

Word (doc, docm, docx)

Excel (csv, xml, xls, xlsx)

PDF (pdf)

Compressed Folder (zip, tar)

https://github.com/experius/Magento-2-Module-Experius-WysiwygDownloads

You can edit di.xml And add Following code

<item name="allowed" xsi:type="array">
    <item name="svg" xsi:type="string">image/svg</item> 
</item>

<item name="image_allowed" xsi:type="array">
    <item name="svg" xsi:type="string">image/svg</item>
</item>

<item name="media_allowed" xsi:type="array">
    <item name="svg" xsi:type="string">image/svg</item>
</item>

and in backend set what you need like this

enter image description here

8

The reason SVG's are disabled in Magento 2 by default, is for security reasons such as stored XSS. And I would recommend that you leave them disabled atleast for now until we get better sanitization for them.

You can see an example of this in APPSEC-1673, where they removed SVGs in the favicon

https://magento.com/security/patches/magento-2016-and-219-security-update

APPSEC-1673 Stored xss using svg images in Favicon.

While you can't upload SVG's via the backend, you can still upload them with FTP and reference them as if you did upload them. This way you still get the benefits of using SVG's but you don't have to deal with the security issues of allowing users to load them.

Enable SVG upload for Favicon/Logo upload

But if you do still want to enable SVG upload for the favicon/logo upload. You can create a custom module and override xml nodes/php functions where it was removed from. You can find these in the diff below

https://github.com/magento/magento2/compare/2.1.8...2.1.9

Enable SVG elsewhere

Dependent on where you want to enable SVG uploads will affect what files you will need to override. You should be able to find the files you need to edit by a quick search for allowedExtensions in XML files or either getAllowedExtensions setAllowedExtensions in PHP files.

5

open OR overwrite below file and code <item name="svg" xsi:type="string">text/html</item>

/vendor/magento/module-cms/etc/di.xml

<type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
    <arguments>
        <argument name="extensions" xsi:type="array">
            <item name="allowed" xsi:type="array">
                ......
                <item name="svg" xsi:type="string">text/html</item>
                ......
            </item>
            <item name="image_allowed" xsi:type="array">
                ......
                <item name="svg" xsi:type="string">text/html</item>
                ......
            </item>
            <item name="media_allowed" xsi:type="array">
                ......
                <item name="svg" xsi:type="string">text/html</item>
                ......
            </item>
        </argument>
    </arguments>
</type>

If you use below magento 2.3 version , code silde change . Replace above code <item name="svg" xsi:type="string">text/html</item> To <item name="svg" xsi:type="number">1</item>

  • still not working File validation failed. and i mention i use magento 2.3.1 version – Savan Patel Jun 17 at 12:49
5

Open Below File

/vendor/magento/module-cms/etc/di.xml

Add your extension you want to allow

<item name="svg" xsi:type="number">1</item>

<argument name="extensions" xsi:type="array">
    <item name="allowed" xsi:type="array">
        <item name="jpg" xsi:type="number">1</item>
        <item name="jpeg" xsi:type="number">1</item>
        <item name="png" xsi:type="number">1</item>
        <item name="gif" xsi:type="number">1</item>
        <item name="svg" xsi:type="number">1</item>
    </item>
    <item name="image_allowed" xsi:type="array">
        <item name="jpg" xsi:type="number">1</item>
        <item name="jpeg" xsi:type="number">1</item>
        <item name="png" xsi:type="number">1</item>
        <item name="gif" xsi:type="number">1</item>
    </item>
    <item name="media_allowed" xsi:type="array">
        <item name="flv" xsi:type="number">1</item>
        <item name="swf" xsi:type="number">1</item>
        <item name="avi" xsi:type="number">1</item>
        <item name="mov" xsi:type="number">1</item>
        <item name="rm" xsi:type="number">1</item>
        <item name="wmv" xsi:type="number">1</item>
    </item>
</argument>
  • Thanks Rakesh But still not working – Savan Patel Jun 17 at 13:43
  • @SavanPatel you have checked your theme path this file override? if override then this code update in file – Rakesh Donga Jun 18 at 4:33
0

Plugin to Allow SVG Files for Product Images

magento2-module-svg-product-images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.