【问题标题】:Rails API create QR code and store image in active_storageRails API 创建二维码并将图像存储在 active_storage
【发布时间】: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

【问题讨论】:

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


【解决方案1】:

我可以通过遵循here 的答案来实现这一点。

简而言之,在您生成 png 后,您可以将其附加到您的模型中,如下所示: @model.qr_code.attach(io: StringIO.new(png.to_s), filename: "filename.png")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多