Rails - 如何即时生成缩略图

发布于 2024-12-31 21:09:45 字数 289 浏览 3 评论 0原文

我正在寻找一种方法来动态生成上传图像的缩略图。我有一个看起来像这样的产品模型:

    class Product
      include Mongoid::Document
      include Mongoid::Paperclip

      has_mongoid_attached_file :picture
    end

我基本上只想上传一张高分辨率图片,然后我可以将其用于缩略图、主图像以及精美盒子的高分辨率图片等。

是有一个简单的方法可以做到这一点吗?

I am looking for a way to generate thumbnails of my uploaded images on the fly. I have a product model that looks kind of like this:

    class Product
      include Mongoid::Document
      include Mongoid::Paperclip

      has_mongoid_attached_file :picture
    end

I basically just want to upload one high-res picture, which I can then use for thumbnails, the main image and also the high-res picture for fancy-box etc..

Is there an easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2025-01-07 21:09:45

您可以为回形针 Attached_file 设置许多属性。

例如,

has_mongoid_attached_file :picture,
    :styles => {
      :original => ['1920x1680>', :jpg],
      :small    => ['100x100#',   :jpg],
      :medium   => ['250x250',    :jpg],
      :large    => ['500x500>',   :jpg]
    }

您可以为多个版本的图像设置 :styles 属性,并具有您想要的尺寸和格式。

请查看此处了解更多详细信息。不过,您确实需要像 RMagick / Imagemagick 这样的图像处理器。

There are many attributes you can set for paperclip attached_file.

e.g.,

has_mongoid_attached_file :picture,
    :styles => {
      :original => ['1920x1680>', :jpg],
      :small    => ['100x100#',   :jpg],
      :medium   => ['250x250',    :jpg],
      :large    => ['500x500>',   :jpg]
    }

You can set the :styles attributes with many versions of the image with the sizes and formats you want.

Check here for more details. You do need image processor like RMagick / Imagemagick though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文