dongzhui2636
dongzhui2636
2018-01-16 03:17
浏览 92
已采纳

在Golang中使用字节数组进行SVG追加/创建

I have an Image with its height, width and parameters. The objective is to create a file with pngs/jpgs into SVG. This SVG has to be portable without any additional files. When I tried with the approach mentioned in the SVGO documentation (code below), the svg takes a link reference to the Image. So when the image is deleted, svg will not have the Image in it. How do I push Image's byte[] into the SVG so that it becomes portable? I have tried the below code.

func createSvgFromImage(ImageObjectStructures []ImageObjectStructure, filename string) {
    filename += ".svg"
    file, err := os.Create(filename)
    if err != nil {
        fmt.Println("svg file creation error:", err)
    }
    width := int(10000)  // Has to be taken from individual page size
    height := int(10000) // Has to be taken from individual page size
    svg := svg.New(file)
    svg.Start(width, height)
    for i := 0; i < len(ImageObjectStructures); i++ {
        svg.Image(ImageObjectStructures[i].XPosition, ImageObjectStructures[i].YPosition, ImageObjectStructures[i].Width, ImageObjectStructures[i].Height, ImageObjectStructures[i].ImagePath, ImageObjectStructures[i].ImageCaption)
        file.Write(ImageObjectStructures[i].ImageData)
    }
    svg.End()
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpx86402
    dpx86402 2018-01-16 04:20
    已采纳

    To embed the image data in the SVG, you'll need to use a Data URL. You Base64-encode the image file data, then append it to a prefix string containing the URL scheme data:, the MIME type (eg. image/png) and the encoding (base64).

    The SVG will end up looking like the following:

    <svg ...>
    
      <image xlink:href="data:image/png;base64,PHN2ZyB4bWxucz0iaHR0cDo...etc..."/>
    
    </svg>
    

    You can learn more about Data URLs here

    点赞 评论

相关推荐