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

时间:2011-05-21 10:48:54

标签: ruby-on-rails file-upload rmagick carrierwave

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

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

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

如何告知carrierwave为上传的照片选择哪种尺寸?所有上传内容都会调整为小型和大型吗?

1 个答案:

答案 0 :(得分:3)

您可以创建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