如何更改base64格式的svg img的颜色?

时间:2014-12-12 12:45:33

标签: javascript html css

我有一个base64格式的Svg img并尝试更改它的颜色。有没有人有任何指示如何做到这一点?我找到了关于<path>元素的内容,但确实svg img位于base64字符串中,我不确定如何使用它。任何人都可以帮助我或指出我正确的方向吗?

&#13;
&#13;
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iNS4wIC0xMC4wIDEwMC4wIDEzNS4wIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAxMDAgMTAwIiB4bWw6c3BhY2U9InByZXNlcnZlIiBoZWlnaHQ9IjEwMHB4IiB3aWR0aD0iMTAwcHgiPgo8cGF0aCBpZD0icGF0aDE5ODNfM18iIGQ9Ik01MC4xLDQuOXY3LjRjMCwwLDAsMC0wLjIsMGMtMC4xLDAtMC40LDAuMi0wLjQsMC40YzAsMC4yLDAsNS4xLDAsNS4zYzAsMC4yLTAuMiwwLjItMC4yLDAuNCAgYzAsMC4yLDAsNC42LDAsNC44cy0wLjIsMC4yLTAuMiwwLjRjMCwwLjIsMCwxLjcsMCwxLjdsLTEuMywzLjZjMCwwLTAuNSwwLTAuNywwYy0wLjIsMC0wLjQsMC4yLTAuNCwwLjRjMCwwLjIsMCwwLjksMCwwLjkgIHMtMC40LDAtMC41LDBzLTAuNCwwLjItMC40LDAuNGMwLDAuMiwwLDUuMSwwLDUuMXMtMC40LDAtMC41LDBjLTAuMiwwLTAuNCwwLjItMC40LDAuNGMwLDAuMiwwLDYsMCw2LjJjMCwwLjMtMC42LDAuNC0wLjYsMC42ICBjMCwwLjIsMCwzMCwwLDMwcy0wLjcsMC0wLjksMGMtMC4yLDAtMC40LDAuMi0wLjQsMC40YzA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BieSBMYXphciBOaWtvbGljPC90ZXh0Pjx0ZXh0IHg9IjAuMCIgeT0iMTIyLjUiIGZvbnQtc2l6ZT0iNS4wIiBmb250LXdlaWdodD0iYm9sZCIgZm9udC1mYW1pbHk9IkhlbHZldGljYSBOZXVlLCBIZWx2ZXRpY2EsIEFyaWFsLVVuaWNvZGUsIEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+">
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:6)

由于您没有澄清是否无法修改原始文件或必须在客户端进行,我建议

以编程方式处理:

  1. 查找<img>
  2. img.src strip "data:image...",提取Base64编码的SVG。
  3. 使用atob
  4. 对其进行解码
  5. 创建一个元素,仅用于查找<path>
  6. 设置路径填充。
  7. 附加新图像或替换当前图像。
  8. 实施例

    // 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,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iNS4wIC0xMC4wIDEwMC4wIDEzNS4wIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAxMDAgMTAwIiB4bWw6c3BhY2U9InByZXNlcn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LTAuNC0wLjRjLTAuMiwwLTAuNSwwLTAuNSwwczAtNSwwLTUuMWMwLTAuMi0wLjItMC40LTAuNC0wLjRjLTAuMiwwLTAuNiwwLTAuNiwwczAtMC43LDAtMC45ICBjMC0wLjItMC4yLTAuNC0wLjQtMC40Yy0wLjIsMC0wLjcsMC0wLjcsMGwtMS4zLTMuNmMwLDAsMC0xLjUsMC0xLjdjMC0wLjItMC4yLTAuMi0wLjItMC40czAtNC42LDAtNC44YzAtMC4yLTAuMi0wLjItMC4yLTAuNCAgYzAtMC4yLDAtNS4xLDAtNS4zYzAtMC4yLTAuMi0wLjQtMC40LTAuNHMtMC4yLDAtMC4yLDBWNC45aC0wLjRoLTAuMUg1MC4xeiIvPgo8dGV4dCB4PSIwLjAiIHk9IjExNy41IiBmb250LXNpemU9IjUuMCIgZm9udC13ZWlnaHQ9ImJvbGQiIGZvbnQtZmFtaWx5PSJIZWx2ZXRpY2EgTmV1ZSwgSGVsdmV0aWNhLCBBcmlhbC1Vbmljb2RlLCBBcmlhbCwgU2Fucy1zZXJpZiIgZmlsbD0iIzAwMDAwMCI+Q3JlYXRlZCBieSBMYXphciBOaWtvbGljPC90ZXh0Pjx0ZXh0IHg9IjAuMCIgeT0iMTIyLjUiIGZvbnQtc2l6ZT0iNS4wIiBmb250LXdlaWdodD0iYm9sZCIgZm9udC1mYW1pbHk9IkhlbHZldGljYSBOZXVlLCBIZWx2ZXRpY2EsIEFyaWFsLVVuaWNvZGUsIEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+">

    注意:

    IE上有一些限制&lt; 10

    Can I use atob?

    编辑:Muplipath方法

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

答案 1 :(得分:1)

使用Google Chrome将data-uri转换为svg文件的技巧是:

  1. 右键单击DevTools中的<img>标记
  2. 使用&#34;在新标签页中打开链接&#34;
  3. 在新标签页中打开data:image...
  4. 右键单击新选项卡中的图像和&#34;查看页面源&#34;
  5. 现在您拥有svg的源代码,您可以轻松更改它,例如在stroke="#f99"上添加path属性,使其显示为粉红色。

    我已将原始<img>转换为<svg>

    &#13;
    &#13;
    <h2>&lt;img&gt; tag<h2> 
    <img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iNS4wIC0xMC4wIDEwMC4wIDEzNS4wIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAxMDAgMTAwIiB4bWw6c3BhY2U9InByZXNlcnZlIiBoZWlnaHQ9IjEwMHB4IiB3aWR0aD0iMTAwcHgiPgo8cGF0aCBpZD0icGF0aDE5ODNfM18iIGQ9Ik01MC4xLDQuOXY3LjRjMCwwLDAsMC0wLjIsMGMtMC4xLDAtMC40LDAuMi0wLjQsMC40YzAsMC4yLDAsNS4xLDAsNS4zYzAsMC4yLTAuMiwwLjItMC4yLDAuNCAgYzAsMC4yLDAsNC42LDAsNC44cy0wLjIsMC4yLTAuMiwwLjRjMCwwLjIsMCwxLjcsMCwxLjdsLTEuMywzLjZjMCwwLTAuNSwwLTAuNywwYy0wLjIsMC0wLjQsMC4yLTAuNCwwLjRjMCwwLjIsMCwwLjksMCwwLjkgIHMtMC40LDAtMC41LDBzLTAuNCwwLjItMC40LDAuNGMwLDAuMiwwLDUuMSwwLDUuMXMtMC40LDAtMC41LDBjLTAuMiwwLTAuNCwwLjItMC40LDAuNGMwLDAuMiwwLDYsMCw2LjJjMCwwLjMtMC42LDAuNC0wLjYsMC42ICBjMCwwLjIsMCwzMCwwLDMwcy0wLjcsMC0wLjksMGMtMC4yLDAtMC40L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AwMCI+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="100px" width="100px">
    <path id="path1983_3_" fill="#f99" d="M50.1,4.9v7.4c0,0,0,0-0.2,0c-0.1,0-0.4,0.2-0.4,0.4c0,0.2,0,5.1,0,5.3c0,0.2-0.2,0.2-0.2,0.4  c0,0.2,0,4.6,0,4.8s-0.2,0.2-0.2,0.4c0,0.2,0,1.7,0,1.7l-1.3,3.6c0,0-0.5,0-0.7,0c-0.2,0-0.4,0.2-0.4,0.4c0,0.2,0,0.9,0,0.9  s-0.4,0-0.5,0s-0.4,0.2-0.4,0.4c0,0.2,0,5.1,0,5.1s-0.4,0-0.5,0c-0.2,0-0.4,0.2-0.4,0.4c0,0.2,0,6,0,6.2c0,0.3-0.6,0.4-0.6,0.6  c0,0.2,0,30,0,30s-0.7,0-0.9,0c-0.2,0-0.4,0.2-0.4,0.4c0,0.2,0,6.6,0,6.6s-1.8,0-2,0c-0.2,0-0.4,0.2-0.4,0.4c0,0.2,0,9.9,0,9.9  s-2.6,0-2.8,0s-0.4,0.2-0.4,0.4c0,0.2,0,4.5,0,4.5h25.9c0,0,0-4.3,0-4.5c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-2.8,0-2.8,0s0-9.7,0-9.9  c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-2,0-2,0s0-6.4,0-6.6c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-0.9,0-0.9,0s0-29.8,0-30c0-0.2-0.6-0.3-0.6-0.6  c0-0.3,0-6.1,0-6.2c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-0.5,0-0.5,0s0-5,0-5.1c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-0.6,0-0.6,0s0-0.7,0-0.9  c0-0.2-0.2-0.4-0.4-0.4c-0.2,0-0.7,0-0.7,0l-1.3-3.6c0,0,0-1.5,0-1.7c0-0.2-0.2-0.2-0.2-0.4s0-4.6,0-4.8c0-0.2-0.2-0.2-0.2-0.4  c0-0.2,0-5.1,0-5.3c0-0.2-0.2-0.4-0.4-0.4s-0.2,0-0.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>
    &#13;
    &#13;
    &#13;

答案 2 :(得分:0)

首先使用自定义function或外部library解码base64编码的图像。然后,您可以编写适当的string replace method以应用于已解码的svg文件。

但仍有一个问题:你尝试了什么?

答案 3 :(得分:0)

对于简单的单色base64编码SVG,您可以通过CSS filters调整颜色。

您需要应用的过滤器有所不同。由于base64编码的SVG是黑色单色的,因此您首先invert将其变成白色。然后正确应用sepiasaturate。最后,您将通过hue-rotate获得的黄色调调整为所需的颜色。

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

答案 4 :(得分:0)

https://base64.guru的帮助下,这对我有用:

  1. 使用https://base64.guru/converter/decode/image/svg将base64代码转换为SVG,下载该SVG,并根据需要对其进行编辑
  2. 使用https://base64.guru/converter/encode/image/svg将已编辑的SVG转换回base64并复制生成的代码

很棒的免费网站,为我节省了很多时间。