如何获得当前载波实例的宽度和高度?
像这样的东西:
car_images.each do | image| image_tag( image.photo_url,:width => image.photo_width,:height => image.photo_height) end
不幸的是,image.photo_width和image.photo_height无效.
我需要指定图像的宽度和高度,这是我正在使用的jquery插件所必需的.
解决方法
结合
https://github.com/jnicklas/carrierwave/wiki/How-to:-Get-version-image-dimensions和
https://github.com/jnicklas/carrierwave/wiki/How-to:-Store-the-uploaded-file-size-and-content-type,你会得到:
class Image before_save :update_image_attributes private def update_image_attributes if image.present? self.content_type = image.file.content_type self.file_size = image.file.size self.width,self.height = `identify -format "%wx%h" #{image.file.path}`.split(/x/) # if you also need to store the original filename: # self.original_filename = image.file.filename end end end