Carrierwave and mini_magick define width and height

After a little investigation, I decided to use Carrierwave and mini_magick in my new rail3 application.

I set it up and it works great. However, I have one question: I would like to have access to the width and height, so I can form the html correctly. However, by default this information is missing, and because of how it stores the data, I really can’t imagine how to add to dB.

Does anyone have any tips or ideas? Is it possible?

+5
source share
3 answers
class Attachment
  mount_uploader :file, FileUploader

  def image
    @image ||= MiniMagick::Image.open(file.path)
  end
end

And use it as follows:

Attachment.first.image['width'] # => 400
Attachment.first.image['height'] # => 300
+14
source

, , Mongo GridFS, :

  def image
    @image ||= MiniMagick::Image.read(Mongo::GridFileSystem.new(Mongoid.database).open(file.path, 'r'))
  end
+2

height/width RMagick MiniMagick :

  • , .

FYI height, width load, <img> jQuery.

:

$(document).ready(function(){   
   var $image = $('.fixed-frame img');
   $image.load(function(){
      rePositionLogo($image);
   });

  if($image.prop('complete')){
    rePositionLogo($image);
  }

});

function rePositionLogo($image){
  var height = $image.height();
  var width = $image.width();
  if (width > height) {
    $image.parents('.header').addClass('landscape');
    var marginTop = (105 - $image.height())/2;
    $image.css('margin-top', marginTop + 'px')
  }else{
    $image.parents('.header').addClass('portrait');
  }
}

, load() , . , cache.

, $('#myImage').prop('complete'), true .

0
source

All Articles