将SVG过滤器应用的图像转换为二进制或base64以保存在后端

Convert SVG filter applied image to binary or base64 to save in backend

本文关键字:base64 保存 后端 存在 二进制 过滤器 SVG 应用 转换 图像      更新时间:2023-11-28

我在GUI中显示了一个图像,并对其应用了SVG过滤器。现在我的要求是通过转换为二进制或base64来保存这个更改后的图像[应用了SVG滤波器]。我尝试使用画布进行转换。但没能实现。以下是我迄今为止所做的尝试。有人能给我一些建议吗。

JsFiddle Link

<div class="item active filtered" data-slide-number="0">
    <svg style="overflow: hidden; height: 637px; width: 546px;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events">
        <defs>
            <filter id="svgBlur" width="110%" height="110%">
                <feComponentTransfer id="bFilter">
                    <feFuncR id="brightness1" class="brgt" type="linear" slope="2"></feFuncR>
                    <feFuncG id="brightness2" class="brgt" type="linear" slope="1"></feFuncG>
                    <feFuncB id="brightness3" class="brgt" type="linear" slope="1"></feFuncB>
                </feComponentTransfer>
                <feComponentTransfer id="cFilter">
                    <feFuncR id="contrast1" class="cnst" type="linear" slope="1" intercept="-0.01"></feFuncR>
                    <feFuncG id="contrast2" class="cnst" type="linear" slope="1" intercept="-0.01"></feFuncG>
                    <feFuncB id="contrast3" class="cnst" type="linear" slope="1" intercept="-0.01"></feFuncB>
                </feComponentTransfer>
                <feComponentTransfer id="gFilter">
                    <feFuncR id="gamma1" class="gama" type="gamma" amplitude="1" exponent="1" offset="0"></feFuncR>
                    <feFuncG id="gamma2" class="gama" type="gamma" amplitude="1" exponent="1" offset="0"></feFuncG>
                    <feFuncB id="gamma3" class="gama" type="gamma" amplitude="1" exponent="1" offset="0"></feFuncB>
                </feComponentTransfer>
                <feColorMatrix id="saturation" type="saturate" values="1"></feColorMatrix>
            </filter>
        </defs>
        <g id="viewport-20160404045307934" class="svg-pan-zoom_viewport" transform="matrix(1.011111111111111,0,0,1.011111111111111,2.52222222222224,0)">
            <image xlink:href="https://www.ricoh.com/r_dc/caplio/r7/img/sample_03.jpg" class="img-responsive" width="90%" id="imageStyling" height="630px" style="height: 630px; width: 536px;" filter="url(#svgBlur)" name="ipsDF14-2-29Feb16-9d80-1a94a2e8c121"></image>
        </g>
    </svg>
</div>
    <button id="save">Save</button>
    <canvas id="canvas"></canvas>

$(document).ready(function() {
    var btn = document.querySelector('button');
    var svg = document.querySelector('svg');
    var canvas = document.querySelector('canvas');
    $("#save").click(function() {
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var data = (new XMLSerializer()).serializeToString(svg);
        var DOMURL = window.URL || window.webkitURL || window;
        var img = new Image();
        var svgBlob = new Blob([data], {
            type: 'image/svg+xml;charset=utf-8'
        });
        var url = DOMURL.createObjectURL(svgBlob);
        alert(JSON.stringify(url));
    })
});

由于安全原因,在许多情况下无法将SVG转换为图像,尤其是当SVG指向外部源时。一些浏览器接受内联资源,如图像和CSS,但即使这样,一些浏览器,如IE,也不允许这样做

选项2是将图像绘制到画布上,并使用上下文的新"filter"属性。目前只有Firefox支持它,但前提是您通过标志(canvas.filters.enabled)启用它。其他供应商对这个想法有些冷淡,所以支持有限。然而,如果这是一个私人项目,那么它可能是一个足够好的解决方案;过滤器允许您直接将过滤器应用于位图,而无需使用相同的定义来遍历SVG。如果这是一个公共项目,你可以选择下面的选项3-

选项3是在代码中实现过滤算法,并将其应用于画布。这是一项更多的工作(实际上相当多),但嘿,我们是开发人员,编码很有趣!!:)

您可以在SVG规范以及其他地方找到过滤器的基础和公式。

或者查看一些已经经历了重重困难的图书馆。