【问题标题】:SVG does not open properly in vector editors or convert properly using ImageMagickSVG 无法在矢量编辑器中正确打开或使用 ImageMagick 正确转换
【发布时间】:2017-10-02 14:37:12
【问题描述】:

我一直在努力在网页中正确调整和缩放 svg 图像的大小并使其正常流动。我决定放弃并将其转换为 png。

<svg
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   version="1.1"
   width="1920" height="165px"
   viewBox="0 0 1920 165"
   >

  <defs>
    <style type="text/css">
      @import url('https://fonts.googleapis.com/css?family=Bitter');
      @import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
      @import url('https://fonts.googleapis.com/css?family=Roboto+Slab');
      @import url('https://fonts.googleapis.com/css?family=Volkhov|Vollkorn');
    </style>
    <style>
      :root {
      --graphics: #670309;
      --logo-text: #282B28;
      --services: #fff;
      --bottom-bar-font-size: 0.8em;
      }
      .house{
        fill: none;
        stroke: var(--graphics);
      }
      .bottom{
        fill: var(--graphics);
        stroke: none;
      }
      .logo{
        fill: var(--logo-text);
        stroke: none;
      }
      .features{
        fill: #282B28;
        stroke: none;
      }

      .services {
          fill: var(--services);
          stroke: none;
          text-anchor: start;
          alignment-baseline="middle"
          font-weight="400"
          font-family="Roboto Condensed"
          letter-spacing="0.03em"
      }

      .service {
        font-size: var(--bottom-bar-font-size);
        font-family="Roboto Condensed";
      }

      .separator {
        font-size: var(--bottom-bar-font-size);
        font-family="Roboto Condensed";
      }
    </style>
  </defs>

  <g id="whole-logo" transform="translate(0,0)scale(1)">
    <svg>
      <!--  ROOF  -->
      <path d="m 5 100 l 90 -50 l 90 50" stroke-width="15" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>

      <!--  CHIMNEY  -->
      <path d="m 155 60 l 0 20" stroke-width="15" stroke-linecap="butt" class="house"/>

      <!--  LEFT WALL -->
      <path d="m 25 90 l 0 60" stroke-width="12" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>

      <!--  RIGHT WALL -->
      <path d="m 165 90 l 0 60" stroke-width="12" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>

      <!--  DOOR  -->
      <rect x="60" y="95" height="45" width="25" class="features"/>

      <!--  SINGLE WINDOW DEFINITION -->
      <defs>
        <rect id="window" x="0" y="0" width="10" height="10" class="features"/>
      </defs>

      <!--  WINDOWS  -->
      <g transform="translate(105,95)">
        <use href="#window" transform="translate(0 0)"/>
        <use href="#window" transform="translate(0 12)"/>
        <use href="#window" transform="translate(12 0)"/>
        <use href="#window" transform="translate(12 12)"/>
      </g>

      <!--  BOTTOM BAR AND TEXT -->
      <g transform="translate(0,140)">
        <svg width="100%" height="25px">
          <rect x="0" y="0" width="100%" height="25px" class="bottom" stroke-width="0"/>
          <text x="1.5em" y="65%" class="services" font-family="Roboto Condensed" >
            <tspan class="service">residential</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">commercial</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">pre-purchase</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">pre-sale</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">warranty</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">expert-witness</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">sewer</tspan>
            <tspan class="separator" dx="5px">&#9632;</tspan>
            <tspan class="service" dx="5px">radon</tspan>
          </text>

        </svg>
      </g>

      <!--  HOUSE DETECTIVES LLC  -->
      <g id="biz-name" transform="translate(265,140)scale(0.8)">
        <line x1="0" x2="500" y1="0" y2="0" style="stroke: green"></line>
        <text dx="70px" dy="0"
          text-anchor="start"
          font-size="38px"
          font-family="Roboto Slab"
          font-weight="bold"
          transform="rotate(-90)"
          class="logo">the</text>
        <text dx="0" dy="0"
          text-anchor="start"
          font-size="78px"
          font-family="Roboto Slab"
          font-weight="bold"
          class="logo">
            <tspan class="name" x="5" dx="0" dy="-73">House</tspan>
            <tspan class="name" x="-70" dx="0" dy="65">Detectives</tspan>
            <tspan class="name" x="0" dx="330" dy="-2" font-size="24px">llc</tspan>
        </text>
      </g>
    </svg>
  </g>
</svg>

我的 svg 在浏览器中渲染得很好,我已经用validator.nu 验证了它。但是,它无法在我尝试过的任何矢量编辑器中正确打开,包括 Inkscape 和其他。例如,Inkscape 看起来像:

它也不能用 ImageMagick 正确转换。

我和drawsvg.org很接近

接下来我应该尝试什么?

【问题讨论】:

    标签: svg png imagemagick-convert


    【解决方案1】:

    百分比值在viewBox 属性中无效。 viewBox 属性的目的是告诉浏览器围绕 SVG 内容的边界框的尺寸。

    对于您的 SVG,suitabel viewBox 将是:

    viewBox="0 0 620 165"
    

    <svg
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       version="1.1"
       width="100%" height="165px"
       viewBox="0 0 620 165"
       >
    
      <defs>
        <style type="text/css">
          @import url('https://fonts.googleapis.com/css?family=Bitter');
          @import url('https://fonts.googleapis.com/css?family=Roboto+Condensed');
          @import url('https://fonts.googleapis.com/css?family=Roboto+Slab');
          @import url('https://fonts.googleapis.com/css?family=Volkhov|Vollkorn');
        </style>
        <style>
          :root {
          --graphics: #670309;
          --logo-text: #282B28;
          --services: #fff;
          --bottom-bar-font-size: 0.8em;
          }
          .house{
            fill: none;
            stroke: var(--graphics);
          }
          .bottom{
            fill: var(--graphics);
            stroke: none;
          }
          .logo{
            fill: var(--logo-text);
            stroke: none;
          }
          .features{
            fill: #282B28;
            stroke: none;
          }
    
          .services {
              fill: var(--services);
              stroke: none;
              text-anchor: start;
              alignment-baseline="middle"
              font-weight="400"
              font-family="Roboto Condensed"
              letter-spacing="0.03em"
          }
    
          .service {
            font-size: var(--bottom-bar-font-size);
            font-family="Roboto Condensed";
          }
    
          .separator {
            font-size: var(--bottom-bar-font-size);
            font-family="Roboto Condensed";
          }
        </style>
      </defs>
    
      <g id="whole-logo" transform="translate(0,0)scale(1)">
        <svg>
          <!--  ROOF  -->
          <path d="m 5 100 l 90 -50 l 90 50" stroke-width="15" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>
    
          <!--  CHIMNEY  -->
          <path d="m 155 60 l 0 20" stroke-width="15" stroke-linecap="butt" class="house"/>
    
          <!--  LEFT WALL -->
          <path d="m 25 90 l 0 60" stroke-width="12" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>
    
          <!--  RIGHT WALL -->
          <path d="m 165 90 l 0 60" stroke-width="12" stroke-linecap="butt" stroke-linejoin="miter" class="house"/>
    
          <!--  DOOR  -->
          <rect x="60" y="95" height="45" width="25" class="features"/>
    
          <!--  SINGLE WINDOW DEFINITION -->
          <defs>
            <rect id="window" x="0" y="0" width="10" height="10" class="features"/>
          </defs>
    
          <!--  WINDOWS  -->
          <g transform="translate(105,95)">
            <use href="#window" transform="translate(0 0)"/>
            <use href="#window" transform="translate(0 12)"/>
            <use href="#window" transform="translate(12 0)"/>
            <use href="#window" transform="translate(12 12)"/>
          </g>
    
          <!--  BOTTOM BAR AND TEXT -->
          <g transform="translate(0,140)">
            <svg width="100%" height="25px">
              <rect x="0" y="0" width="100%" height="25px" class="bottom" stroke-width="0"/>
              <text x="1.5em" y="65%" class="services" font-family="Roboto Condensed" >
                <tspan class="service">residential</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">commercial</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">pre-purchase</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">pre-sale</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">warranty</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">expert-witness</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">sewer</tspan>
                <tspan class="separator" dx="5px">&#9632;</tspan>
                <tspan class="service" dx="5px">radon</tspan>
              </text>
    
            </svg>
          </g>
    
          <!--  HOUSE DETECTIVES LLC  -->
          <g id="biz-name" transform="translate(265,140)scale(0.8)">
            <line x1="0" x2="500" y1="0" y2="0" style="stroke: green"></line>
            <text dx="70px" dy="0"
              text-anchor="start"
              font-size="38px"
              font-family="Roboto Slab"
              font-weight="bold"
              transform="rotate(-90)"
              class="logo">the</text>
            <text dx="0" dy="0"
              text-anchor="start"
              font-size="78px"
              font-family="Roboto Slab"
              font-weight="bold"
              class="logo">
                <tspan class="name" x="5" dx="0" dy="-73">House</tspan>
                <tspan class="name" x="-70" dx="0" dy="65">Detectives</tspan>
                <tspan class="name" x="0" dx="330" dy="-2" font-size="24px">llc</tspan>
            </text>
          </g>
        </svg>
      </g>
    </svg>

    【讨论】:

    • @Paul——注意你的渲染不正确。底部带有文本的矩形被截断。我已经尝试使用 javascript 以编程方式设置 viewBoxviewBox 是(四舍五入的)"0 0 16 149",对应于 165 的高度。我已将 svg 编辑为 viewBox"0 0 1920 165"但看不到任何改善。
    • 对不起。我的 viewBox 有点错误。我已经更正了。
    • 不幸的是,这无助于在 inkscape 中打开或使用 imagemagick 进行转换。同样的问题。
    • 我试图帮助您获得响应式 SVG,这样您就不必将其转换为 PNG。它不会转换为 PNG 的原因是其他原因。这些将包括这样一个事实,即它们几乎肯定不支持您正在使用的最新 CSS 功能。例如字体导入和 CSS 参数。也可能不支持alignment-baseline。如果您真的想要 PNG,为什么不直接在浏览器中加载并截屏?
    • @Paul--我想要至少 1920 像素的宽分辨率。我不认为截图会给我。但事实证明你是完全正确的。实际上,我有一个 FHD 屏幕,它的宽度是 3690 像素!我更喜欢它 SVG 以便我可以轻松地使用颜色,但至少这是一个解决方案。
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多