我正在使用minimagick
将 SVG 图像动态调整为 PNG 图像。
这是我的代码:
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')
myFont
图像被渲染,但它是 512x512 图像(视口大小),并且在渲染中未使用字体。
知道如何解决这个问题吗?