我正在使用回形针宝石上传图片,我想上传横向和纵向图片。可以帮助我如何设置两个图像的尺寸。
我的代码是:
has_attached_file :media,
:styles => {:yplarge=>"440x300>"},
:path => ":rails_root/public/system/:class/:id/:style/:basename.:extension",
:url => "/system/buzz_pictures/:id/:style/:basename.:extension"
validates_attachment_size :media, :less_than => 2.megabytes,
:message => "Please attach a smaller picture."
validates_attachment_content_type :media,
:content_type=>['image/jpeg', 'image/png', 'image/gif']
此代码适用于风景图像,但不适用于肖像。
答案 0 :(得分:0)
只需添加另一种风格:
:styles => {
:yplarge=>"440x300>",
:portrait=>"300X440>"
}
根据需要更改值。请注意,如果图像小于给定尺寸,则不会调整尺寸。要更改该行为,请将>
替换为#
。这将强制将图像调整为指定的尺寸。
请参阅Paperclip文档以了解使用不同的样式:
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
答案 1 :(得分:0)
下面的解决方案将保存2种样式,如果横向原始,则纵向旋转90,反之亦然。
has_attached_file :media,
:styles => {:landscape => Proc.new { |a| { :geometry => "440x300>", :rotation => 90 unless a.landscape? } },
:portrait => Proc.new { |a| { :geometry => "300x440>", :rotation => 90 if a.landscape? } } }
:path => ":rails_root/public/system/:class/:id/:style/:basename.:extension",
:url => "/system/buzz_pictures/:id/:style/:basename.:extension",
:processors => [:rotator]
def landscape?
Paperclip::Geometry.from_file(to_file(:original)).horizontal?
end
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
super + rotate_command
else
super
end
end
def rotate_command
if @options[:rotation]
" -rotate #{ @options[:rotation] }"
end
end
end
end