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
- Create the di.xml file at Vendor\Module\etc
1234567<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"><type name="Magento\Framework\File\Uploader"><plugin sortOrder="1" name="MeetanshiAddSvg" type="Vendor\Module\Plugin\File\UploaderPlugin"/></type></config> - Create UploaderPlugin.php file at Vendor\Module\Plugin\File
12345678910111213141516171819202122232425<?phpnamespace Vendor\Module\Plugin\File;use Magento\Framework\App\Action\Action;use Magento\Framework\App\Action\Context;class UploaderPlugin extends Action{public function __construct(Context $context){parent::__construct($context);}public function aroundSetAllowedExtensions(\Magento\Framework\File\Uploader $subject, \Closure $proceed, $extensions = []){if (!in_array('svg', $extensions)) {$extensions[] = 'svg';}return $proceed($extensions);}public function execute(){}}
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.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
16 Comments
Hai,
Is this applicable for magento 2.4.3 version?
Hello Udhai,
Yes, it works fine with Magento 2.4.3.
Thank You
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 ?
Hello Jenul,
For the frontend, we used UploaderPlugin.php file from di.xml.
You need to find the same for the backend.
Thank You
After adding this code, I am getting below error.
Disallowed file type.
Hello Gaurav,
The above code is properly working from our end.
Which image type you are trying to upload?
Thank You.
Hi
from this code we can add webp extension for uploading images
Hello,
Yes, you can add webp extension for uploading images.
Just replace SVG with webp in the above code.
Thanks
Thanks Sanjay Jethva for Information
Hello Moazzam Ali,
Glad to know that it’s useful for you!
Thank You
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
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
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.”
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.
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
Hello,
Add the below code after aroundSetAllowedExtensions() function
Thanks