回形针没有正确调整尺寸

时间:2014-10-08 21:57:00

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip

我正在为我的应用程序使用Paperclip,图像不符合我设置的尺寸。我正在使用paperclip和rmagick并安装了ImageMagik。当我跑转换时我得到了

C:\Program Files\ImageMagick-6.8.9-Q16\convert.exe
C:\Windows\System32\convert.exe

Development.rb

Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/convert.exe"

的Gemfile

gem 'paperclip'
gem 'rmagick', '~> 2.13.2', :platforms => :ruby

User.rb

has_attached_file :avatar, styles: { medium: '210x260>', larger: "300x300>", thumb: "100x100>" }, default_url: "/assets/default.png"
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

视图

<%= image_tag @user.avatar(**style**) %>

我正在两个不同尺寸的图像上测试它,A(最初:960x688)和B(最初:160x160)

A(:拇指)变为100x72

B(:拇指)变为100x100

A(:medium)变为210x151

B(:中等)变为160x160

A(:较大)变为300x300

B(:更大)变为300x300

我在尝试更改大小后尝试重新上传图片但获得了相同的结果。那么,回形针是否存在不同尺寸图像的问题,或者我的代码是否存在问题?

1 个答案:

答案 0 :(得分:1)

您需要在尺寸标注后更改尾随字符,因此在您需要的情况下需要

medium: 210x260#

文件明确说明

Default behavior is to resize the image and maintain aspect ratio (i.e. the :medium version of a 300×150 image will be 200×100). Some commonly used options are:

trailing '#', thumbnail will be centrally cropped, ensuring the requested dimensions.
trailing '>', thumbnail will only be modified if it is currently larger requested dimensions. (i.e. the :small thumb for a 120×80 original image will be unchanged)

对于您当前的图像,您可以在控制台中进行此操作

Image.all.each {|s| s.image.reprocess! }

之后,您上传的任何其他图片都不需要重新处理,并会根据您的需要调整大小

希望有所帮助

相关问题