9

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)
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
David
  • 607
  • 1
  • 8
  • 12

3 Answers3

12

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
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
James Chen
  • 10,794
  • 1
  • 41
  • 38
  • 1
    +1 This method is a lot cleaner and is similar to the one recommended by RMagick here: http://www.imagemagick.org/RMagick/doc/comtasks.html – iwasrobbed Dec 21 '11 at 02:31
  • I am not sure about this here, but let me recommend .ping instead of .read (from http://stackoverflow.com/a/16267106/4738391) – Zia Ul Rehman Mughal Nov 07 '16 at 07:13
7

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

glebm
  • 20,282
  • 8
  • 51
  • 67
  • Thanks! The the solution marked as the answer didn't work for me. Nice & simple solution. – Kyle Carlson Jun 07 '13 at 17:28
  • Please note that this will only capture the biggest version's size. You can use https://github.com/gzigzigzeo/carrierwave-meta for multiple version support (it adds process like the other examples). Also this is probably not very efficient since I seem to be using `binread` here, which reads the whole file instead of just the header – glebm Jun 08 '13 at 04:00
1

i googled some around a came onto a post with the following:

source link http://groups.google.com/group/carrierwave/browse_thread/thread/c5e93b45bde8a85e?fwc=1&pli=1

class HeaderUploader < CarrierWave::Uploader::Base 
   storage :right_s3 
  def store_dir 
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
  end 
  def url 
    ["http://#{s3_bucket}.s3.amazonaws.com/", path].compact.join 
  end 
   before :cache, :capture_size_before_cache 
   before :retrieve_from_cache, :capture_size_after_retrieve_from_cache 
  def capture_size_before_cache(new_file) 
    model.header_width, model.header_height = `identify -format "%wx 
 %h" #{new_file.path}`.split(/x/) 
  end 
  def capture_size_after_retrieve_from_cache(cache_name) 
    model.header_width, model.header_height = `identify -format "%wx 
%h" #...@file.path}`.split(/x/) 
  end 
  def dimensions 
    "#{model.header_width}x#{model.header_height}" 
  end 
end 
rik.vanmechelen
  • 1,904
  • 17
  • 24