使用回形针宝石,我想根据图像是横向还是纵向改变应用于上传图像的每种样式的尺寸。
基本上是这样的:
styles => {
:original => (uploaded_image.width > uploaded_image.height) ? "1000x800>" : :800x1000>",
:medium => (uploaded_image.width > uploaded_image.height) ? "600x400>" : :400x600>",
:thumb => "100x100#"
}
这可能吗?如果是,如何实现呢?
谢谢
没有找到像我原来问题中包含的伪代码那样简单的语法,但我确实找到了一种相当直接的方法来做到这一点。这是我的模型中的代码。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