【问题标题】:How to scale to fit image inside svg如何缩放以适合svg中的图像
【发布时间】:2023-03-11 13:16:01
【问题描述】:

我正在尝试修改 svg 以使其包含的图像具有 svg 的全部宽度和高度。我该怎么做?

我正在尝试将宽度和高度修改为 100%,x,y 为“0”并将此 svg 属性设置为 preserveAspectRatio="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;
}
&lt;!--?xml version="1.0" encoding="UTF-8" standalone="no" ?--&gt;&lt;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"&gt;&lt;desc&gt;Created with Fabric.js 1.6.7&lt;/desc&gt;&lt;defs&gt;&lt;/defs&gt;&lt;g transform="translate(776.8 529.9) scale(1.01 0.95)"&gt;&lt;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"&gt;&lt;/image&gt;&lt;/g&gt;	&lt;g id="1554312687241" transform="translate(781 762.29)"&gt;		&lt;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;"&gt;			&lt;tspan x="-164" y="13.54" fill="#ffffff"&gt;Votre entreprise&lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;	&lt;g id="1554312687289" transform="translate(779 847.3)"&gt;		&lt;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;"&gt;			&lt;tspan x="-68.31" y="6.3" fill="#ffffff"&gt;Votre adresse &lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;	&lt;g id="1554312687296" transform="translate(776 811.17)"&gt;		&lt;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;"&gt;			&lt;tspan x="-68.83" y="5.67" fill="#ffffff"&gt;Votre telephone&lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;&lt;/svg&gt;

我一直在尝试做的事情:

body {
  padding: 10vw 30vw;
}

svg {
  border: 5px solid;
}
&lt;!--?xml version="1.0" encoding="UTF-8" standalone="no" ?--&gt;&lt;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"&gt;&lt;g transform="translate(776.8 529.9) scale(1.01 0.95)"&gt;&lt;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"&gt;&lt;/image&gt;&lt;/g&gt;	&lt;g id="1554312687241" transform="translate(781 762.29)"&gt;		&lt;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;"&gt;			&lt;tspan x="-164" y="13.54" fill="#ffffff"&gt;Votre entreprise&lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;	&lt;g id="1554312687289" transform="translate(779 847.3)"&gt;		&lt;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;"&gt;			&lt;tspan x="-68.31" y="6.3" fill="#ffffff"&gt;Votre adresse &lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;	&lt;g id="1554312687296" transform="translate(776 811.17)"&gt;		&lt;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;"&gt;			&lt;tspan x="-68.83" y="5.67" fill="#ffffff"&gt;Votre telephone&lt;/tspan&gt;		&lt;/text&gt;	&lt;/g&gt;&lt;/svg&gt;

我期待这个结果:

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 的图像 url,结果与我期望的不同,我不明白为什么。

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 问题,最终的图像并不好。

【问题讨论】:

  • 您需要在此处发布您的标记和代码,而不是任何第三方网站。 minimal reproducible example
  • 您希望我们研究这三个 CodePen 示例并发现 SVG 中的差异吗?
  • 我已经在 stackoverflow sn-p 中添加了代码,对此感到抱歉。唯一的区别是 svg 中的图像。我添加了一些解释以使其更易于理解。

标签: svg image-resizing


【解决方案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) 与图像/视图框 (616x960) 的纵横比不同。

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

  1. 您可以通过将preserveAspectRatio="none" 添加到 SVG 来拉伸 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. 您可以通过将preserveAspectRatio="xMidYMid slice" 添加到 SVG 来安排放大 viewBox 区域以适应更宽的 SVG 尺寸(相当于 HTML 的 background-size: cover)。

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>

【讨论】:

    【解决方案2】:

    您遇到的问题是图像具有宽透明边框。您要么需要使用类似 Photoshop 的软件来切断该边框,要么您可以这样做:

    body {
      padding: 10vw 30vw;
    }
    
    svg {
      border: 5px solid;
    }
    <svg viewBox="-304 -506 608 1005" >
        <g id="theg">
          <image id="1554312687213" xlink:href="https://lobservateur-avesnois.easypubphr.fr/wp-content/uploads/sites/6/2018/11/restaurantmodel.png" x="-733" y="-750" width="1461" height="1564"></image>
    </g>
        <g id="theText" text-anchor="middle" fill="#ffffff">
            <text font-family="ABeeZee" font-size="43" font-style="normal" font-weight="bold" >Votre entreprise</text>
          <text font-family="ABeeZee" y="30" font-size="20" >Votre adresse</text>
          <text font-family="ABeeZee" font-size="18" y="60" >Votre telephone</text>
        </g>
    </svg>

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      相关资源
      最近更新 更多