简体   繁体   English

SVG 转换<g>标记为<img>包含 base64 PNG URI 的标签(阅读更多)</g>

[英]SVG convert <g> tag to an <image> tag containing base64 PNG URI (read more)

I got an interesting problem (I hope!) I have noticed that there are two "types" of SVGs我遇到了一个有趣的问题(希望如此!)我注意到 SVG 有两种“类型”

First we have the conventional SVG file with and tags for example:首先,我们有带有 和 标签的常规 SVG 文件,例如:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 841.89 1190.55" style="enable-background:new 0 0 841.89 1190.55;" xml:space="preserve">
     <g id="Background">
        <rect id="Color1" class="st3" width="840.94" height="1190.55"/>
        <g id="Texture" class="st4">
            <path class="st5" d="M843.67,410.13c-73.29 ...

Secondly we have embedded tags in the image, not sure what to call them, so I've just named them "fake SVG", an example:其次,我们在图像中嵌入了标签,不知道该怎么称呼它们,所以我只是将它们命名为“假 SVG”,例如:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="3718" height="4899" viewBox="0 0 3718 4899">
  <image id="Lager_1" data-name="Lager 1" width="185.9" height="244.95000000000002" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUh ...

My question is: Is there is any smooth way to convert a conventional SVG into a "fake" SVG?我的问题是:有没有什么流畅的方法可以将传统的 SVG 转换为“假” SVG? (please tell me if they have a real name). (请告诉我他们是否有真实姓名)。 Also keep in mind that I would like to keep the groupings so each <g> should convert to a <image> tag还要记住,我想保留分组,所以每个<g>都应该转换为<image>标签

My thoughts: I am thinking about loading the conventional SVG into a <canvas> tag, it seems to be able to understand the <g> groupings in the conventional SVG well and consistently, and from there, somehow, convert those groups individually into base64 PNG URI, and reconstruct it into a fake SVG, perhaps there should be some library out there that can help out, does anyone have any ideas?我的想法:我正在考虑将传统的 SVG 加载到<canvas>标签中,它似乎能够很好地理解传统 SVG 中的<g>分组,并且从那里以某种方式将这些组单独转换为 Z745A1446A78ABB PNG URI,并将其重建为假 SVG,也许应该有一些库可以提供帮助,有人有什么想法吗?

I'm not sure if this will exactly answer what you are trying to do, but you can do this:我不确定这是否能准确回答你想要做的事情,但你可以这样做:

<image id="something" href='data:image/svg+xml;utf8,<svg version="1.1" xmlns="http://www.w3.org/2000/svg" .../>' />

Where you can just embed the original SVG as a string in the href when the encoding is set to data:image/svg+xml;utf8 .当编码设置为data:image/svg+xml;utf8时,您可以将原始 SVG 作为字符串嵌入到 href 中。

This will render the SVG in an <image> tag, but not in Base64 (I could not get this working, but there may be a way).这将在<image>标记中呈现 SVG,但不在 Base64 中(我无法使其正常工作,但可能有办法)。

Source: https://css-tricks.com/probably-dont-base64-svg/资料来源: https://css-tricks.com/probably-dont-base64-svg/

It is possible to convert an inline SVG with tags in the DOM to an image data-url.可以将带有 DOM 中标签的内联 SVG 转换为图像数据 URL。 You just need to turn it into one like this:你只需要把它变成这样的:

imageTag.src = 'data:image/svg+xml,' + window.escape(svgTag.outerHTML);

The imageTag version can not have external links embedded in the SVG. imageTag 版本不能在 SVG 中嵌入外部链接。 It also needs width and height attributes on the SVG tag otherwise it will not show in some browsers.它还需要 SVG 标签上的宽度和高度属性,否则它不会在某些浏览器中显示。 You could also draw this image to a canvas to rasterize the image, but as the comment mentioned that would only make it uglier if scaled.您也可以将此图像绘制到 canvas 以对图像进行光栅化,但正如评论所述,如果缩放只会使其更丑陋。

If you want to convert it to a SVG file you should add this as a first line and then add the outerHTML of the svgTag.如果要将其转换为 SVG 文件,则应将其添加为第一行,然后添加 svgTag 的 outerHTML。

<?xml version="1.0" encoding="UTF-8"?>

You can save the file with the.svg extension您可以使用 .svg 扩展名保存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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