开发者

Using Paperclip gem, how to modify style dimensions applied to an uploaded based on orientation?

开发者 https://www.devze.com 2023-04-02 20:49 出处:网络
Working with the Paperclip gem and I want to change the dimensions for each style applied to an uploaded image differently depending on whether the image is in landscape or 开发者_开发知识库portrait o

Working with the Paperclip gem and I want to change the dimensions for each style applied to an uploaded image differently depending on whether the image is in landscape or 开发者_开发知识库portrait orientation.

Basically something like this:

styles => {
:original => (uploaded_image.width > uploaded_image.height) ? "1000x800>" : :800x1000>",
:medium => (uploaded_image.width > uploaded_image.height) ? "600x400>" : :400x600>",
:thumb => "100x100#"
}

Is this possible? And if so, how to make it happen?

Thanks


Didn't find a syntax as simple as the pseudo-code included in my original question, but I did find a fairly straight-forward way to do this. Here's the code in my model.rb file:

  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#"
                }

and then this "resize_cover_image" method as such:

     # 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

If anyone has something better or any helpful comments on how to make this better ... I'm all ears.

Thanks -wg

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号