【发布时间】:2020-12-08 14:16:41
【问题描述】:
我正在使用 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
)
我还应该使用生成 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 文件生成图像的方法。
谢谢。
注意:前端是 react-native
【问题讨论】:
-
以防万一您不必存储图像,您可以使用帮助程序即时生成二维码。这是如何做到的。 leighhalliday.com/creating-qr-codes-in-rails
标签: ruby-on-rails imagemagick qr-code rails-activestorage