I'm sending a SVG string from browser (Javascript) to a server that is running Rails (Ruby). I want to convert this string to a PNG with transparency or, at least, a SVG file so I can convert it later
Any ideas? I installed RMagick but I'm still not sure how to create the file from the string.
Any other solution to achieve this?
The idea is to create simple "logos" dynamically
svg file, right click on it and click on the context menu item 'Save SVG as PNG. Lets you click on the extension icon or right click on an . svg file and choose Save SVG as PNG. You will be able to specify the desired width of the rendered PNG image.
With RMagick, just read the string into an image instance with Image.from_blob + defining the SVG format
require "RMagick"
svg_string='
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120" viewPort="0 0 120 120" version="1.1">
<rect width="150" height="150" fill="rgb(0, 255, 0)" stroke-width="1" stroke="rgb(0, 0, 0)" />
<line x1="20" y1="100" x2="100" y2="20" stroke="black" stroke-width="2"/>
</svg>'
img = Magick::Image.from_blob(svg_string) {
self.format = 'SVG'
self.background_color = 'transparent'
}
img.write "example_out.png"
Edit
If the string is just a SVG path, there's Magick::Draw.path
to "rebuild" vector graphics. Doc & examples here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With