RailsCasts Pro episodes are now free!

Learn more or hide this

rwwaskk's Profile

GitHub User: rwwaskk

Site: www.yunrui.guru

Comments by

Avatar

You can get the dimension of the img using
img.columns, img.rows

I ended up doing something like this to prevent blurring of the cropped image:

ruby
def crop
  if model.crop_x.present?

    #resize_to_limit(300, 3000)

    manipulate! do |img|
      converted_h = 300.0*img.rows/img.columns
      x = (model.crop_x.to_i/300.0*img.columns).to_i
      y = (model.crop_y.to_i/converted_h*img.rows).to_i
      w = (model.crop_w.to_i/300.0*img.columns).to_i
      h = (model.crop_h.to_i/converted_h*img.rows).to_i
      #img.crop("#{w}x#{h}+#{x}+#{y}") 

      img.crop!(x, y, w, h)
      img
     end
  end
end

**Noticed this was from 2 years ago...