0

我正在使用回形针 gem 上传图像,我想同时上传横向和纵向图像。任何人都可以帮助我如何设置这两个图像的尺寸。

我的代码是:

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']

此代码适用于横向图像,但不适用于纵向。

4

2 回答 2

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
于 2012-05-28T15:30:49.523 回答
0

只需添加另一种样式:

:styles => {
  :yplarge=>"440x300>",
  :portrait=>"300X440>"
}

根据需要更改值。请注意,如果图像小于给定尺寸,则不会调整其大小。要更改该行为,请将 替换>#。这将强制将图像调整为指定的尺寸。

有关使用不同样式的信息,请参阅回形针文档:

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

于 2012-05-28T15:04:56.073 回答