使用minimagick和载波调整图像大小

时间:2015-12-15 21:33:08

标签: css ruby-on-rails twitter-bootstrap

我正在尝试使用载波将图像上传到博客。在博客上,无论照片的大小如何,我希望所有图像都适合相同尺寸的框。我想他们所有人都是450x253。这是我的代码:

载波的上传器文件:

   class PhotoUploader < CarrierWave::Uploader::Base


  include CarrierWave::MiniMagick
  include Sprockets::Rails::Helper
  storage :fog

 def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
 end 

   process :resize_to_fill => [450, 253]

  # Create different versions of  files:
    version :thumbnail do
      process :resize_to_fill => [200, 112]
    end

    version :profile_size do
      process :resize_to_fill => [450, 253]
   end

   version :full_size do
     process :resize_to_fill => [568, 320]
   end

  def extension_white_list
     %w(jpg jpeg gif png)
  end

end

我的index.html.erb文件:

           <div class="row">
                    <% @articles.each do |article| %>
                    <div class="col-md-6">
                        <div class="well well-lg">
                                <p><%= image_tag(article.image.url) %>

                                    <div class="body">
                                        <div class="text-center">
                                            <%= article.description %>
                                        </div>
                                          <div class="row">
                                            <div class="col-xs-3">
                                                <button class="btn btn-primary btn-lg" type="submit">Button</button>
                                            </div>
                                            <div class="col-xs-9">
                                                <button class="btn btn-default btn-lg pull-right" type="submit">Button</button>
                                            </div>
                                         </div>
                                      </div>
                                </div>
                             </div>
                        <% end %>
                    <div class="col-md-6 col-sm-6">
                    </div>
                </div>

在这一行:

  <%= image_tag(article.image.url) %>

我也尝试过:

  <%= image_tag(article.image.profile_size.url) %>

因为我的上传程序文件说profile_size应该呈现450x253,但这仍然不起作用。使用我当前的代码,页面上显示的照片大小与我上传的大小相同,根本不会调整大小,例如:

photo of my screenshot

我希望它们的大小与照片左边的大小相同,但是自动调整大小(照片中的那个大小自然是那么大),我该怎么做?我已经尝试过这样看:http://www.rubydoc.info/github/jnicklas/carrierwave/CarrierWave/MiniMagick

在这里:Carrierwave - Resizing images to fixed width

感谢

1 个答案:

答案 0 :(得分:2)

使用resize_to_fit或resize_to_limit而不是resize_to_fill

process :resize_to_limit => [450, 253]

process :resize_to_fit => [450, 253]