ruby-on-rails - Rails API 创建二维码并将图像存储在 active_storage 中

标签 ruby-on-rails imagemagick qr-code rails-activestorage

我正在研究 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/

相关文章:

ruby-on-rails - 使用 bundle exec 运行 Rails 控制台

mysql - 如何在 MySQL/Rails 中获取列的随机默认值

image-processing - iOS 上的 ImageMagick : Blurring and blending an image with itself

.NET 兼容的二维码扫描设备

flutter - 使用 flutter 扫描二维码的问题

sql - PostgreSQL 从 jsonb 中提取 key ,异常 "cannot call jsonb_object_keys on a scalar"

ruby - 如何修改图像的颜色以消除活力?

imagemagick - 如何在Windows 7上安装ImageMagick(二)

java - ZXing 如何使用手电筒启动扫描仪?

iOS “Add to Home Screen” - 文件下载将用户踢出