如何编写回形针样式以将上传图像的宽度保持在 100%,但仅将高度裁剪为宽度的 60%?
像这样的东西:
has_attached_file :image, :styles => { :cropped => "100%x[60% of height]" }
如何编写回形针样式以将上传图像的宽度保持在 100%,但仅将高度裁剪为宽度的 60%?
像这样的东西:
has_attached_file :image, :styles => { :cropped => "100%x[60% of height]" }
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
谢谢
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
希望这可以帮助你