如何缩放以适合svg

时间:2019-04-04 11:42:05

标签: svg image-resizing

我正在尝试修改svg,以使其持有的图像占据svg的整个宽度和高度。我该怎么办?

我正在尝试将宽度和高度修改为100%,x,y设置为“ 0”,并将此svg属性设置为saveAspectRatio =“ xMidYMid Meet”。

这是初始svg:

<?xml version="1.0" encoding="UTF-8"?>
<svg
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1512" height="1063" viewBox="0 0 1512 1063" xml:space="preserve">
    <desc>Created with Fabric.js 1.6.7</desc>
    <defs></defs>
    <g transform="translate(776.8 529.9) scale(1.01 0.95)">
        <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" width="1461" height="1564" preserveAspectRatio="none"></image>
    </g>
    <g id="1554312687241" transform="translate(781 762.29)">
        <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
            <tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>
        </text>
    </g>
    <g id="1554312687289" transform="translate(779 847.3)">
        <text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
            <tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>
        </text>
    </g>
    <g id="1554312687296" transform="translate(776 811.17)">
        <text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
            <tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>
        </text>
    </g>
</svg>

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?--><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="219" height="302" viewBox="0 0 1512 1063" xml:space="preserve"><desc>Created with Fabric.js 1.6.7</desc><defs></defs><g transform="translate(776.8 529.9) scale(1.01 0.95)"><image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" width="1461" height="1564" preserveAspectRatio="none"></image></g>	<g id="1554312687241" transform="translate(781 762.29)">		<text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>		</text>	</g>	<g id="1554312687289" transform="translate(779 847.3)">		<text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>		</text>	</g>	<g id="1554312687296" transform="translate(776 811.17)">		<text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>		</text>	</g></svg>

我一直在尝试做的事情:

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?--><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="219" height="302" viewBox="0 0 1512 1063" xml:space="preserve" preserveAspectRatio="xMidYMid meet"><g transform="translate(776.8 529.9) scale(1.01 0.95)"><image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="0" y="0" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" width="100%" height="100%" preserveAspectRatio="none"></image></g>	<g id="1554312687241" transform="translate(781 762.29)">		<text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>		</text>	</g>	<g id="1554312687289" transform="translate(779 847.3)">		<text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>		</text>	</g>	<g id="1554312687296" transform="translate(776 811.17)">		<text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">			<tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>		</text>	</g></svg>

我希望得到这个结果:

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<svg width="300px" height="300px" viewBox="0 0 579 375" preserveAspectRatio="xMidYMid slice">
  
  <image xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9674/photo-1501366062246-723b4d3e4eb6.jpg" x="0" y="0" width="100%" height="100%"></image>
  
</svg>

但是,如果我只是更改期望svg的图片网址,结果将与我所期望的不同,我不理解为什么。

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<svg width="300px" height="300px" viewBox="0 0 579 375" preserveAspectRatio="xMidYMid slice">
  
  <image xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="0" y="0" width="100%" height="100%"></image>
  
</svg>

最终目标是下载png中的svg。转换为png的代码已完成,但是由于此svg问题,最终图像效果不佳。

2 个答案:

答案 0 :(得分:1)

您遇到的问题是图像的边框透明。您或者需要使用类似Photoshop的软件来剪裁该边框,也可以这样操作:

null
body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}

答案 1 :(得分:1)

您需要调整viewBox属性。您目前拥有的那个太大了。您的重要内容在该区域的中间占一小部分。

您当前的viewBox是:

viewBox="0 0 1512 1063"

与背景图片的非透明部分匹配的更好的viewBox是:

viewBox="472 16 616 960"

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="219" height="302" viewBox="472 16 616 960" xml:space="preserve">
  <desc>Created with Fabric.js 1.6.7</desc>
  <defs></defs>
  <g transform="translate(776.8 529.9) scale(1.01 0.95)">
    <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" width="1461" height="1564" preserveAspectRatio="none"></image>
  </g>
  <g id="1554312687241" transform="translate(781 762.29)">
    <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>
    </text>
  </g>
  <g id="1554312687289" transform="translate(779 847.3)">
    <text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>
    </text>
  </g>
  <g id="1554312687296" transform="translate(776 811.17)">
    <text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>
    </text>
  </g>
</svg>

但是,在此示例中,它仍然不完全适合。那是因为您的SVG尺寸(219 x 302)与image / viewBox(616x960)的宽高比不同。

您可以通过多种方式解决此问题:

  1. 您可以通过向SVG添加preserveAspectRatio="none"来扩展viewBox区域,以适应更大的SVG尺寸。

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="219" height="302" viewBox="472 16 616 960" preserveAspectRatio="none">
  <desc>Created with Fabric.js 1.6.7</desc>
  <defs></defs>
  <g transform="translate(776.8 529.9) scale(1.01 0.95)">
    <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" width="1461" height="1564" preserveAspectRatio="none"></image>
  </g>
  <g id="1554312687241" transform="translate(781 762.29)">
    <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>
    </text>
  </g>
  <g id="1554312687289" transform="translate(779 847.3)">
    <text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>
    </text>
  </g>
  <g id="1554312687296" transform="translate(776 811.17)">
    <text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>
    </text>
  </g>
</svg>

  1. 您可以通过向SVG添加background-size: cover来扩大viewBox区域以适应更宽的SVG尺寸(相当于HTML的preserveAspectRatio="xMidYMid slice")。

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="219" height="302" viewBox="472 16 616 960" preserveAspectRatio="xMidYMid slice">
  <desc>Created with Fabric.js 1.6.7</desc>
  <defs></defs>
  <g transform="translate(776.8 529.9) scale(1.01 0.95)">
    <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" width="1461" height="1564" preserveAspectRatio="none"></image>
  </g>
  <g id="1554312687241" transform="translate(781 762.29)">
    <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>
    </text>
  </g>
  <g id="1554312687289" transform="translate(779 847.3)">
    <text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>
    </text>
  </g>
  <g id="1554312687296" transform="translate(776 811.17)">
    <text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>
    </text>
  </g>
</svg>

  1. 您可以通过减少width属性来使SVG变窄。一个更正确的值是:

    302 * (616/960) ~= 194
    

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
<!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="194" height="302" viewBox="472 16 616 960">
  <desc>Created with Fabric.js 1.6.7</desc>
  <defs></defs>
  <g transform="translate(776.8 529.9) scale(1.01 0.95)">
    <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-730.5" y="-782" width="1461" height="1564" preserveAspectRatio="none"></image>
  </g>
  <g id="1554312687241" transform="translate(781 762.29)">
    <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-164" y="13.54" fill="#ffffff">Votre entreprise</tspan>
    </text>
  </g>
  <g id="1554312687289" transform="translate(779 847.3)">
    <text font-family="ABeeZee" font-size="20" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.31" y="6.3" fill="#ffffff">Votre adresse </tspan>
    </text>
  </g>
  <g id="1554312687296" transform="translate(776 811.17)">
    <text font-family="ABeeZee" font-size="18" font-style="normal" font-weight="normal" text-decoration="normal" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;">
      <tspan x="-68.83" y="5.67" fill="#ffffff">Votre telephone</tspan>
    </text>
  </g>
</svg>

相关问题