urj*_*ils 6 qr-code ruby-on-rails imagemagick rails-activestorage
我正在使用 Rails 6 API,需要生成 QR 码图像并使用 active_storage 将该图像保存到 S3。
我为此使用 rqrcode gem,它为我提供了 SVG 字符串。如何将 SVG 字符串转换为图像并使用 active_storage 将该图像存储到 S3?
以下是我的代码
控制器
qrcode = RQRCode::QRCode.new("#{@order.id}")
svg = qrcode.as_svg(
offset: 0,
color: '000',
shape_rendering: 'crispEdges',
module_size: 6,
standalone: true
)
Run Code Online (Sandbox Code Playgroud)
我还应该使用生成 png 图像
png = qrcode.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: 'black',
file: nil,
fill: 'white',
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 120
)
Run Code Online (Sandbox Code Playgroud)
但是在存储时出现以下错误
ArgumentError (Could not find or build blob: expected attachable, got <ChunkyPNG::Image 120x120 [
Run Code Online (Sandbox Code Playgroud)
寻找一种从 svg 字符串或 png 文件生成图像的方法。
谢谢。
注意:前端是react-native