Carrier Wave Image Size

How can I get the width and height of the current instance of the carrier?

Something like that:

car_images.each do | image| image_tag( image.photo_url, :width => image.photo_width, :height => image.photo_height) end 

Unfortunately, image.photo_width and image.photo_height do not work. I need to specify the width and height of the images, this is necessary for the jquery plugin that I use.

+7
source share
4 answers

Combine https://github.com/jnicklas/carrierwave/wiki/How-to:-Get-version-image-dimensions and https://github.com/jnicklas/carrierwave/wiki/How-to:-Store-the -uploaded-file-size-and-content-type and you will get:

 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 
+13
source

You can easily save height and width as attributes with your model if you use Rmagick. In the Carrierwave bootloader:

 class ArtworkUploader < CarrierWave::Uploader::Base def geometry @geometry ||= get_geometry end def get_geometry if @file img = ::Magick::Image::read(@file.file).first geometry = { width: img.columns, height: img.rows } end end end 

And in your model:

 class Artwork < ActiveRecord::Base mount_uploader :image, ArtworkUploader before_save :save_image_dimensions private def save_image_dimensions if image_changed? self.image_width = image.geometry[:width] self.image_height = image.geometry[:height] end end end 
+11
source

Or just use FastImage . This greatly simplifies retroactively measuring attachments.

+1
source
Answer to

@JamieD worked for me, with one exception. I used the MiniMagick.

So, I added something similar to my bootloader.

 def geometry @geometry ||= get_geometry end def get_geometry if @file img = ::Magick::Image::read(@file.file).first geometry = { width: img.columns, height: img.rows } end end 
0
source

All Articles