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

时间:2015-07-07 07:34:05

标签: php svg png inkscape rsvg

我使用此代码将svg转换为png图片

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

这适用于单个svg图像。

实际上我有svg图像,其中包含多层图像,如:

第一层 - :这是透明的背景T恤图片

第二层 - :这是另一个包含颜色的T恤图片

第3层 - :这是贴在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 个答案:

答案 0 :(得分: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文件。

答案 1 :(得分:0)

你得到的问题来自两个链接的PNG,而不是从该网站解析(即使它们在那里)。例如,如果您要将“dothejob.in”中的这两个图像本地保存在一个名为img的文件夹中,然后将它们放入SVG标记中的xlink:href中作为img / test.png和img / unnamed.png而不是会发现你的命令工作正常。

这也给其他一些人带来了不便,并且有一些不同的解决方案正在讨论中。有关更多信息,请查看stackoverflow上其他地方的this thread。你如何解决这个问题在很大程度上取决于你将使用多少svgs,处理它们需要多快等等。