4

I need to enable SVG image in upload for custom module.

I have tried all the solution which i have found.

Here is my code:

/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>

Any idea? Help will be appreciated.

5

2 Answers 2

1

Here you can find a free solution that allows uploading SVG image all over Magento 2: https://github.com/MagestyApps/module-web-images

0

For magento 2.3 version in etc directory in your module create di.xml file with code

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\MediaStorage\Model\File\Validator\NotProtectedExtension">
        <plugin sortOrder="1" name="RemoveSvgFromNotProtectedExtension" type="Vendor\Module\Plugin\Model\File\Validator\NotProtectedExtension"/>
    </type>
    <virtualType name="Magento\Catalog\CategoryImageUpload" type="Magento\Catalog\Model\ImageUploader">
        <arguments>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="svg" xsi:type="string">svg</item>
            </argument>
            <argument name="allowedMimeTypes" xsi:type="array">
                <item name="svg" xsi:type="string">image/svg</item>
            </argument>
        </arguments>
    </virtualType>
</config>

and create plugin file Vendor\Module\Plugin\Model\File\Validator\NotProtectedExtension.php with code

<?php

namespace Vendor\Module\Plugin\Model\File\Validator;

class NotProtectedExtension
{


/**
 * Remove svg from Not Protected Extensions
 * 
 * @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $extension
 * @param array $result
 * @return array
 */
public function afterGetProtectedFileExtensions(
    \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $extension,
    $result
) {
    if (in_array('svg', $result)) {
        unset($result['svg']);
    }
    return $result;
}
}

thats all)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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