使用 Carrierwave,我想根据上传的照片类型将照片调整为不同的尺寸

发布于 2024-11-08 16:34:40 字数 292 浏览 0 评论 0原文

我有一个用户模型和一个帖子模型。用户的照片将被调整为小缩略图,帖子的照片将被调整为大缩略图。

version :smallThumb do
     process :resize_to_fill => [100, 100]
   end

   version :largeThumb do
     process :resize_to_fill => [200, 200]
   end

如何告诉 Carrierwave 为上传的照片选择哪种尺寸?对于所有上传,它都会调整为小和大吗?

I have a User model and a Post model. A user's photo will be resized to a small thumbnail, and a post's photo will be resized to a large thumbnail.

version :smallThumb do
     process :resize_to_fill => [100, 100]
   end

   version :largeThumb do
     process :resize_to_fill => [200, 200]
   end

How do I tell carrierwave which size to choose for an uploaded photo? Will it resize to both small and large for all uploads?

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

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

发布评论

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

评论(1

悟红尘 2024-11-15 16:34:40

您可以创建 2 个单独的上传器模型。看起来像这样:

profile_uploader.rb

class ProfileUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  version :thumb do
    process :resize_to_fill => [200, 200]
  end

end

atached_uploader.rb

class AttachedUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  version :thumb do
    process :resize_to_fill => [100, 100]
  end

end

user.rb

class User < ActiveRecord::Base

  mount_uploader :profile, ProfileUploader

end

post.rb

class Post < ActiveRecord::Base

  mount_uploader :attached, AttachedUploader

end

You can create 2 separate uploader models. Would look something like this:

profile_uploader.rb

class ProfileUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  version :thumb do
    process :resize_to_fill => [200, 200]
  end

end

atached_uploader.rb

class AttachedUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  version :thumb do
    process :resize_to_fill => [100, 100]
  end

end

user.rb

class User < ActiveRecord::Base

  mount_uploader :profile, ProfileUploader

end

post.rb

class Post < ActiveRecord::Base

  mount_uploader :attached, AttachedUploader

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