使用 Paperclip gem,我想根据图像是横向还是纵向,以不同方式更改应用于上传图像的每种样式的尺寸。
基本上是这样的:
styles => {
:original => (uploaded_image.width > uploaded_image.height) ? "1000x800>" : :800x1000>",
:medium => (uploaded_image.width > uploaded_image.height) ? "600x400>" : :400x600>",
:thumb => "100x100#"
}
这可能吗?如果是这样,如何实现?
谢谢
最佳答案
没有找到像我原来的问题中包含的伪代码一样简单的语法,但我确实找到了一种相当直接的方法来做到这一点。这是我的 model.rb 文件中的代码:
has_attached_file :cover_image,
:path => "events/:created_at/event_:model_id/uploads/:basename_:style.:extension",
:default_url => "placeholders/default_cover_image_:style.png",
:styles => {
:original => Proc.new { |instance| instance.resize_cover_image('original') },
:iphone => Proc.new { |instance| instance.resize_cover_image('iphone') },
:thumb => "150x150#"
}
然后是这样的“resize_cover_image”方法:
# will resize each paperclip style and set orientation attribute when working on the :original style
def resize_cover_image(style)
geo = Paperclip::Geometry.from_file(cover_image.to_file(:original))
case style
when 'original'
if geo.horizontal?
self.cover_image_orientation = 'landscape'
"1360x910>"
else
self.cover_image_orientation = 'portrait'
"910x1360>"
end
when 'iphone'
geo.horizontal? ? "480x318>" : "318x480>"
end
end
如果有人有更好的东西或对如何让它变得更好有任何有用的评论......我洗耳恭听。
谢谢-wg
关于ruby - 使用 Paperclip gem,如何根据方向修改应用于上传的样式尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7330797/