简体   繁体   中英

how can I change color of an svg img in base64 format?

I have an Svg img with in a base64 format and trying to change the color of it. Does anyone have any pointers of how to do this? I found something about the <path> element, but do to the fact that the svg img is in a base64 string I'm not sure of how to use it. Can anyone help me or point me in the right direction?

 <img src="data:image/svg+xml;base64,+Q3JlYXRlZCBieSBMYXphciBOaWtvbGljPC90ZXh0Pjx0ZXh0IHg9IjAuMCIgeT0iMTIyLjUiIGZvbnQtc2l6ZT0iNS4wIiBmb250LXdlaWdodD0iYm9sZCIgZm9udC1mYW1pbHk9IkhlbHZldGljYSBOZXVlLCBIZWx2ZXRpY2EsIEFyaWFsLVVuaWNvZGUsIEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+">

As you do not clarified if can't modify the original file or if must do it on client-side, I propose a

Programmatically approach:

  1. Lookup the <img>
  2. From img.src strip "data:image..." , to extract Base64 encoded SVG.
  3. Decode it using atob
  4. Create an element, just for find the <path>
  5. Set fill on path.
  6. Append the new Image or Replace current one.

Example:

 // Lookup the <img> by Id var img = document.getElementById("target"); // Strip "data:image/svg+xml;base64," to get encoded SVG var encoded = img.src.substring(26); // Decode base64 var decoded = atob(encoded); // Create an HTML element from decoded SVG var wrapper = document.createElement('div'); wrapper.innerHTML = decoded; var newSvg = wrapper.firstChild; // Lookup the <path> and get a ref var innerPath = newSvg.getElementsByTagName('path')[0]; // Set up new color innerPath.setAttribute("fill", "#FF0000"); // Show modified image document.body.appendChild(newSvg);
 <img id="target" src="data:image/svg+xml;base64,+Q3JlYXRlZCBieSBMYXphciBOaWtvbGljPC90ZXh0Pjx0ZXh0IHg9IjAuMCIgeT0iMTIyLjUiIGZvbnQtc2l6ZT0iNS4wIiBmb250LXdlaWdodD0iYm9sZCIgZm9udC1mYW1pbHk9IkhlbHZldGljYSBOZXVlLCBIZWx2ZXRpY2EsIEFyaWFsLVVuaWNvZGUsIEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+">

Note:

There are some restrictions on IE < 10

Can I use atob?

EDIT: Muplipath Approach

var paths = newSvg.getElementsByTagName('path');
[].forEach.call(paths, function(path) {
    path.setAttribute("fill", "#FF0000");
});

For a simple unicolor base64 encoded SVG, you can tweak the color via CSS filters .

The filters you need to apply vary. As your base64 encoded SVG is in black unicolor you first invert it to white. Then apply sepia and saturate it properly. Lastly, you tweak the yellow tone you now have via hue-rotate to your desired color.

img {
  filter: invert(1) sepia(100%) saturate(10000%) hue-rotate(45deg);
}

Start by decoding the image which is base64-encoded using either a custom function or an external library . You can then write a suitable string replace method to apply to the decoded svg file.

But one question remains: what have you tried?

A trick to do convert a data-uri into an SVG file using Google Chrome is to:

  1. Right click on the <img> tag in the DevTools
  2. Open the data:image... in a new tab using "Open Link in New Tab"
  3. Right click on the image in the new tab and "View Page Source"

Now you have the source code of the SVG you can easily change it, for example by modifying the fill="#f99" attribute on the path to make it appear pink.

I've put the original <img> and converted <svg> below.

 <h2>&lt;img&gt; tag<h2> <img src="data:image/svg+xml;base64,+Q3JlYXRlZCBieSBMYXphciBOaWtvbGljPC90ZXh0Pjx0ZXh0IHg9IjAuMCIgeT0iMTIyLjUiIGZvbnQtc2l6ZT0iNS4wIiBmb250LXdlaWdodD0iYm9sZCIgZm9udC1mYW1pbHk9IkhlbHZldGljYSBOZXVlLCBIZWx2ZXRpY2EsIEFyaWFsLVVuaWNvZGUsIEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+"> <h2>&lt;svg&gt; tag<h2> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="5.0 -10.0 100.0 135.0" enable-background="new 0 0 100 100" xml:space="preserve" height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.2,0V4.9h-0.4h-0.1H50.1z"/> <text x="0.0" y="117.5" font-size="5.0" font-weight="bold" font-family="Helvetica Neue, Helvetica, Arial-Unicode, Arial, Sans-serif" fill="#000000">Created by Lazar Nikolic</text><text x="0.0" y="122.5" font-size="5.0" font-weight="bold" font-family="Helvetica Neue, Helvetica, Arial-Unicode, Arial, Sans-serif" fill="#000000">from the Noun Project</text></svg>

This worked for me with the help of https://base64.guru :

  1. Use https://base64.guru/converter/decode/image/svg to convert the base64 code to an SVG, download that SVG, edit it as necessary
  2. Use https://base64.guru/converter/encode/image/svg to convert your edited SVG back to base64 and copy the resulting code

Fantastic free site, saved a lot of time for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM