How to make SVG for PNG rendering with MiniMagick not to ignore font size and options?

I use minimagick to dynamically resize SVG images in PNG.

Here is my code:

 require "mini_magick" svgString = '<?xml version="1.0" encoding="UTF-8"?>'\ '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" viewBox="0, 0, 512, 512">'\ '<g id="Calque_1">'\ ' <g>'\ ' <path d="M256,432.5 C143.61,432.5 52.5,345.867 52.5,239 C52.5,132.133 143.61,45.5 256,45.5 C368.39,45.5 459.5,132.133 459.5,239 C459.5,345.867 368.39,432.5 256,432.5 z" fill="#FFFFFF"/>'\ ' <path d="M256,432.5 C143.61,432.5 52.5,345.867 52.5,239 C52.5,132.133 143.61,45.5 256,45.5 C368.39,45.5 459.5,132.133 459.5,239 C459.5,345.867 368.39,432.5 256,432.5 z" fill-opacity="0" stroke="#000000" stroke-width="1"/>'\ ' </g>'\ ' <text transform="matrix(1, 0, 0, 1, 179.125, 208)">'\ ' <tspan x="-28.125" y="10.5" font-size="24" fill="#000000">Is this using my font?</tspan>'\ ' </text>'\ ' </g>'\ '</svg>' image = MiniMagick::Image.read(svgString) image.combine_options do |b| b.resize "760x760" b.font "/Users/foo/Library/Fonts/myFont.ttf" end image.format("png", 1) image.write('from-string.png') 

The image receives the rendered image, but the 512x512 image (viewport size) and the myFont font myFont not used in the rendering.

Any idea how I could fix this?

+4
source share
2 answers

I found a way to get what I wanted using mini_magick this way (only available from version 4+):

 # Write SVG in a temporary file temp_svg = Tempfile.new('bar') temp_svg.write(svg_string) temp_svg.close MiniMagick::Tool::Mogrify.new do |mogrify| mogrify.resize "760x760" mogrify.font "/Users/foo/Library/Fonts/myFont.ttf" mogrify.format "png" mogrify << temp_svg.path end temp_svg.unlink # The result is in (temp_svg.path + ".png") 

This is terribly ugly since I need to access the file system for both SVG and PNG. But this is the only way to make it work.

+1
source

Not sure if this will help debug your problem, but if you delete the language specific stuff around your SVG line and save it in a file called image.svg , you can check it using the ImageMagick command line:

 <?xml version="1.0" encoding="UTF-8"?> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" viewBox="0, 0, 512, 512"> <g id="Calque_1"> <g> <path d="M256,432.5 C143.61,432.5 52.5,345.867 52.5,239 C52.5,132.133 143.61,45.5 256,45.5 C368.39,45.5 459.5,132.133 459.5,239 C459.5,345.867 368.39,432.5 256,432.5 z" fill="#FFFFFF"/> <path d="M256,432.5 C143.61,432.5 52.5,345.867 52.5,239 C52.5,132.133 143.61,45.5 256,45.5 C368.39,45.5 459.5,132.133 459.5,239 C459.5,345.867 368.39,432.5 256,432.5 z" fill-opacity="0" stroke="#000000" stroke-width="1"/> </g> <text transform="matrix(1, 0, 0, 1, 179.125, 208)"> <tspan x="-28.125" y="10.5" font-size="24" fill="#000000">Is this using my font?</tspan> </text> </g> </svg> 

like this:

 convert -resize 760x760 -font "AdobeDevanagariBI" image.svg image.png 

and you will get the following:

enter image description here

which the using the font I specified (I don’t have one), and also 760x760.

Hope this helps. My conclusion is that either your ImageMagick is out of date or MiniMagick has an error. Sorry, I have no idea how to debug a MiniMagick - can you understand what it actually does under the covers? Is there a debugger?

Version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-06 http://www.imagemagick.org

+1
source

All Articles