Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving image height with CarrierWave

I need the ability to put the processed image's dimensions.

I have in my ImageUploader class:

version :post do
  process :resize_to_fit => [200, nil]
end

Is there a way that I could get the image's dimensions similar to this?

height = @picture.image_height(:post)
like image 344
David Avatar asked Dec 21 '11 02:12

David


2 Answers

You can adjust and use the method described here: http://code.dblock.org/carrierwave-saving-best-image-geometry

It adds a process then call Magick's method to fetch image geometry.

Code:

  version :post do
    process :resize_to_fit => [200, nil]
    process :get_geometry

    def geometry
      @geometry
    end
  end

  def get_geometry
    if (@file)
      img = ::Magick::Image::read(@file.file).first
      @geometry = [ img.columns, img.rows ]
    end
  end
like image 68
James Chen Avatar answered Oct 17 '22 23:10

James Chen


You can hook onto the :cache and :retrieve_from_cache methods

There is no need to rely on system commands either:

# Somewhere in your uploader:
attr_reader :geometry
after :cache, :capture_size
after :retrieve_from_cache, :capture_size
def capture_size(*args)    
  img = ::MiniMagick::Image::read(File.binread(@file.file))
  @geometry = [img[:width], img[:height]]
end

http://www.glebm.com/2012/05/carrierwave-image-dimensions.html

like image 27
glebm Avatar answered Oct 17 '22 23:10

glebm