使用PHP将SVG转换为具有多个图像层的PNG

6

我用这段代码将 svg 转换为 png 图像:

<?php 
exec('/usr/bin/rsvg-convert -w 1000 -h 1000 tshirt.svg -o tshirt.png');
?>

这适用于单个svg图像。

实际上,我有一个包含多层图像的svg图像,如下:

第一层 -:这是背景T恤图像,其为透明的

第二层 -:这是另一个带有颜色的T恤图像

第三层 -:这是小贴纸图像,应该放在T恤上

我的svg代码是 -:

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

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg crossOrigin="anonymous" width="1000px" height="1000px" version="1.1"   xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

    <g class="canvas_background">
        <rect class="canvas_variant_color" width="998" height="998" x="0" y="0" style="" fill="#008080"/>
        <rect real_size="16,22" height="547" class="canvas_border" width="343" y="160" x="335" fill="#008080" />        
    </g>

    <g class="canvas_objects" style="" mask="url('#Sibnip5tjg')">
        <g style="display: block;" transform="matrix(1,0,0,1,-146.5,-236.3909)">
            <image style="display: block; opacity: 1;" height="175" width="308" y="461" x="501" crossOrigin="anonymous" xlink:href="http://dothejob.in/teerrific/img/front/unnamed.png"/>
        </g>
    </g>

    <g class="canvas_mockups">
        <g class="canvas_styles">
            <g class="canvas_style">
                <g  style="opacity: 1;">
                    <image  xlink:href="http://dothejob.in/teerrific/img/front/test.png"  x="0" y="0" width="1000" height="1000" />
                </g>
            </g>
        </g>
    </g> 
</svg>

现在我想将所有的svg图像层合并成一个单独的png图像。
现在我的转换后的png图像仅显示背景色,T恤和贴纸图像未显示。
2个回答

1
请安装inkscape扩展。
然后将您在SVG中使用的图像放置在保存SVG文件的同一文件夹中。
然后像这样更改SVG文件中的图像路径。
 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg crossOrigin="anonymous" width="1000px" height="1000px" version="1.1"   xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

    <g class="canvas_background">
        <rect class="canvas_variant_color" width="998" height="998" x="0" y="0" style="" fill="#008080"/>
        <rect real_size="16,22" height="547" class="canvas_border" width="343" y="160" x="335" fill="#008080" />        
    </g>

    <g class="canvas_objects" style="" mask="url('#Sibnip5tjg')">
        <g style="display: block;" transform="matrix(1,0,0,1,-146.5,-236.3909)">
            <image style="display: block; opacity: 1;" height="175" width="308" y="461" x="501" crossOrigin="anonymous" xlink:href="unnamed.png"/>
        </g>
    </g>

    <g class="canvas_mockups">
        <g class="canvas_styles">
            <g class="canvas_style">
                <g  style="opacity: 1;">
                    <image  xlink:href="test.png"  x="0" y="0" width="1000" height="1000" />
                </g>
            </g>
        </g>
    </g> 
</svg>

之后运行inkscape命令

exec( 'inkscape --without-gui --export-png=all.png tshirt.svg' ); 

然后您将在同一文件夹中获得PNG文件。

0
你遇到的问题来自于两个链接的 PNG 图片无法从该网站解析出来(尽管它们存在)。例如,如果您将这两个图像从“dothejob.in”本地保存在名为 img 的文件夹中,然后只需将它们作为 img/test.png 和 img/unnamed.png 放入 SVG 标记中的 xlink:href 中,那么您会发现您的命令可以正常工作。
这已经给其他一些人带来了不便,因此正在讨论几种不同的解决方案。在 stackoverflow 其他地方查看 此线程 获取更多信息。您的解决方法主要取决于您将处理多少个 SVG,需要多快处理它们等因素。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接