How to Upload SVG Image in Magento 2 Custom Module

Magento 2 allows uploading JPG and PNG images by default. But it does not offer the feature to upload SVG, PDF, and, HTML files.

However, a store owner often requires to upload vector images in the backend as well as in the frontend. It is possible to upload an SVG file using a custom module.

The store owner may want to upload SVG images owing to its multiple benefits such as:

  • SVG images are resolution-independent, i.e., it retains the same quality on every screen resolution or size.
  • The SVG image can result in smaller file size compared to other image types.
  • A JPG image might appear blurry in some displays, but an SVG image still displays in high quality on every screen.
  • The use of an SVG image eliminates the HTTP request that needs to load in an image file. Hence, it results in fewer loading times for a page as no file needs to download.

To leverage these benefits, learn the programmatic method to upload SVG image in Magento 2 custom module.

Additionally, for example, the store offers a customization option that facilitates users to add their personalized design of the product by uploading an image. Now, what if the customer has an image in the SVG format? In that case, it’s a bad practice if a customer has to convert SVG files into JPG or PNG images manually.

They may even skip the purchase if they don’t know the image conversion or the competitor is supporting the SVG image format.

In order to avoid it, one may use the below solution.

Method to Upload SVG Image in Magento 2 Custom Module

  1. Create the di.xml file at Vendor\Module\etc

  2. Create UploaderPlugin.php file at Vendor\Module\Plugin\File

That’s it.

Any doubts? Do mention them in the Comments section below.

I would be glad to help you out.

Also, please share the solution with Magento Community via social media.

Thank you.

4.8
(based on 12 Reviews)
How to Upload SVG Image in Magento 2 Custom Module

Ronak Bhatti

Ronak is a Magento Developer at Meetanshi. He loves playing cricket. Apart from the work, he is an enthusiastic person who loves learning new skills.

16 Comments

  • udhai

    Hai,

    Is this applicable for magento 2.4.3 version?

  • Jenul Savani

    Hello there greetings for the day guys m getting Disallowed file type. while uploading logo image from backend do we need to remove another backend validation when upload svg image ?

    • Sanjay Jethva

      Hello Jenul,
      For the frontend, we used UploaderPlugin.php file from di.xml.
      You need to find the same for the backend.
      Thank You

  • Gaurav Agrawal

    After adding this code, I am getting below error.

    Disallowed file type.

    • Sanjay Jethva

      Hello Gaurav,
      The above code is properly working from our end.
      Which image type you are trying to upload?
      Thank You.

  • moazzam ali

    Hi
    from this code we can add webp extension for uploading images

    • Sanjay Jethva

      Hello,
      Yes, you can add webp extension for uploading images.
      Just replace SVG with webp in the above code.
      Thanks

      • Moazzam Ali

        Thanks Sanjay Jethva for Information

        • Sanjay Jethva

          Hello Moazzam Ali,
          Glad to know that it’s useful for you!
          Thank You

          • Moazzam Ali

            Hi Sanjay Jethva
            when we upload the webp image that error is occur
            “tb_new.webp was not uploaded.
            Something went wrong while saving the file(s).”
            can you help me brother

          • Sanjay Jethva

            Hello Moazzam Ali,
            Regarding your query about .webp file, we’ve checked by uploading .webp and it’s working.
            You need to replace ‘svg’ by ‘webp’.
            Thank You

          • Moazzam Ali

            when we di compile the site then that another error occurs during complication
            “Class Custom\Extension\Plugin\File\Data does not exist
            Class Custom\Extension\Plugin\File\UploaderPlugin\Interceptor generation error: The re
            quested class did not generate properly, because the ‘generated’ directory permission
            is read-only. If — after running the ‘bin/magento setup:di:compile’ CLI command when
            the ‘generated’ directory permission is set to write — the requested class did not
            generate properly, then you must add the generated class object to the signature of th
            e related construct method, only.”

          • Sanjay Jethva

            Hello Moazzam Ali,
            It’s an error of di:compile command.
            The .php file must be there at the path that has been mentioned in di.xml file.
            and check the namespace in your .php file too.
            Plus, provide the permission in var generated pub/static folder.
            Thank You.

  • Dimitri

    Hello,

    got Fatal error. :

    UploaderPlugin contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
    i have M2.3

    Regards

    • Sanjay Jethva

      Hello,

      Add the below code after aroundSetAllowedExtensions() function

      Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *