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()
}
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