我正在研究 Rails 6 API,我需要在其中生成 QR 代码图像并使用 active_storage 将该图像保存到 S3。
我为此使用了 rqrcode gem,它为我提供了 SVG 字符串。如何将 SVG 字符串转换为图像并使用 active_storage 将该图像存储到 S3?
以下是我的代码
Controller
qrcode = RQRCode::QRCode.new("#{@order.id}")
svg = qrcode.as_svg(
offset: 0,
color: '000',
shape_rendering: 'crispEdges',
module_size: 6,
standalone: true
)
我还应该使用生成 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
)
但是在存储时出现以下错误ArgumentError (Could not find or build blob: expected attachable, got <ChunkyPNG::Image 120x120 [
寻找一种从 svg 字符串或 png 文件生成图像的方法。谢谢。
注意:前端是 native react
最佳答案
我能够通过遵循答案 here 来实现这一目标.
简而言之,在您生成 png
之后,您可以将其附加到您的模型,如下所示:@model.qr_code.attach(io: StringIO.new(png.to_s), filename: "filename.png")
关于ruby-on-rails - Rails API 创建二维码并将图像存储在 active_storage 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65200640/