Rails api active_storage

Rails api active_storage

有没有办法在上传时创建图像变体并存储变体而不是整个图像。

例如。用户上传了一张 1000x1000 的图片,当我在 rails 中收到该上传时,我想创建 3 个变体,一个 100x100、一个 300x300 和一个 600x600,并将它们存储在 S3 中,然后可以通过执行类似 post.image.thumbnail

每次请求图像时,我都必须从 s3 检索完整尺寸的图像并即时处理它以将其提供给用户,这似乎很愚蠢。

我想你在找 minimagick

将此添加到您的 gemfile

gem 'mini_magick'

然后就可以使用minimagick的方法对图像进行变换

<%= image_tag user.avatar.variant(resize: "100x100") %>

here is the documentation for this