{"id":1772,"date":"2021-05-18T09:04:05","date_gmt":"2021-05-18T09:04:05","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/upload-svg-image-in-magento-2-custom-module\/"},"modified":"2025-07-16T17:49:38","modified_gmt":"2025-07-16T12:19:38","slug":"upload-svg-image-in-magento-2-custom-module","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/upload-svg-image-in-magento-2-custom-module\/","title":{"rendered":"How to Upload SVG Image in Magento 2 Custom Module"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0allows uploading JPG and PNG images by default. But it\u00a0does not offer the feature to upload SVG, PDF, and, HTML files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The store owner may want to upload SVG images owing to its multiple benefits such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SVG images are resolution-independent, i.e., it retains the same quality on every screen resolution or size.<\/li>\n\n\n\n<li>The SVG image can result in smaller file size compared to other image types.<\/li>\n\n\n\n<li>A JPG image might appear blurry in some displays, but an SVG image still displays in high quality on every screen.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To leverage these benefits, learn the programmatic method to&nbsp;upload SVG image in Magento 2 custom module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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\u2019s a bad practice if a customer has to convert SVG files into JPG or PNG images manually.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They may even skip the purchase if they don\u2019t know the image conversion or the competitor is supporting the SVG image format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to avoid it, one may use the below solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Upload SVG Image in Magento 2 Custom Module<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create the&nbsp;<strong>di.xml<\/strong>&nbsp;file at&nbsp;<strong><strong>Vendor\\Module\\etc<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"..\/..\/..\/..\/..\/lib\/internal\/Magento\/Framework\/ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Framework\\File\\Uploader\">\n        &lt;plugin sortOrder=\"1\" name=\"MeetanshiAddSvg\" type=\"Vendor\\Module\\Plugin\\File\\UploaderPlugin\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>UploaderPlugin.php<\/strong>&nbsp;file at&nbsp;<strong><strong>Vendor\\Module\\Plugin\\File<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Plugin\\File;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\n\nclass UploaderPlugin extends Action\n{\n    public function __construct(Context $context)\n    {\n        parent::__construct($context);\n    }\n\n    public function aroundSetAllowedExtensions(\\Magento\\Framework\\File\\Uploader $subject, \\Closure $proceed, $extensions = [])\n    {\n        if (!in_array('svg', $extensions)) {\n            $extensions[] = 'svg';\n        }\n        return $proceed($extensions);\n    }\n\n    public function execute()\n    {\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, please share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0allows uploading JPG and PNG images by default. But it\u00a0does not offer the feature to upload SVG, PDF, and, HTML files. However, a store&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1772","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1772"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1772\/revisions"}],"predecessor-version":[{"id":18074,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1772\/revisions\/18074"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}