Avatar Cropping with Carrierwave and MiniMagick

I need to implement an image cropping feature on user uploaded avatars. The ever gold RailsCasts episode shows us the way.

All sweet stuff, until MiniMagick tells me it’s mogrify shell command cannot find the image resource. Backtrace points to image.crop(x, y, w, h) line. After some messing around and examining the MiniMagick source. Here’s the fix.

manipulate! do |image|
  x = model.crop_x.to_i
  y = model.crop_y.to_i
  w = model.crop_w.to_i
  h = model.crop_h.to_i
  image.crop("#{w}x#{h}+#{x}+#{y}")
  image
end

Problem solved by changing the image.crop method argument from x, y, w, h to an interpolated string that complies to Imagemagick’s geometry format spec.

EDIT: while writing this post, I found the exact solution has been mentioned on RailsCasts episode’s comments section. Damn, I want my time back :`(

Published: 2015-03-12
blog comments powered by Disqus