回形针:宽度为100%,高度为宽度的60%

时间:2013-08-09 05:37:39

标签: ruby-on-rails ruby-on-rails-3 imagemagick paperclip

我如何编写回形针样式以保持上传图像的宽度为100%但仅将高度裁剪为宽度的60%?

这样的事情:

has_attached_file :image, :styles => { :cropped => "100%x[60% of height]" }

2 个答案:

答案 0 :(得分:1)

has_attached_file :image, :styles => after_save :save_image_dimensions

def save_image_dimensions
  geo = Paperclip::Geometry.from_file(image.path)
  self.image_height = (geo.height.to_i * 60)/100 
end

如果你对提取维度有问题从下面你可以得到一个很好的帮助

https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions

请使用回形针查看此链接以裁剪图像

http://viget.com/extend/manual-cropping-with-paperclip

由于

答案 1 :(得分:1)

has_attached_file :image, :styles => { 
                      :original  => "100x60>",
                      :thumb => Proc.new { |instance| instance.resize }
                    }
  #### End Paperclip ####

  def resize     
     geo = Paperclip::Geometry.from_file(image.to_file(:original))
     height = (geo.width.to_i * 60)/100
     width = geo.width     
     "#{width.round}x#{height.round}!"    
  end  

希望这可以帮到你

相关问题