Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Carrierwave Cropping

By: a guest on Apr 9th, 2012  |  syntax: None  |  size: 0.75 KB  |  views: 124  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class ImageUploader < CarrierWave::Uploader::Base
  2.  
  3.   include CarrierWave::MiniMagick
  4.   ...
  5.   version :t do
  6.     process :cropper
  7.     process :resize_to_fill => [75, 75]
  8.   end
  9.   ...
  10.   def cropper
  11.     manipulate! do |img|
  12.       img = img.crop "#{model.crop_x}x#{model.crop_y}+#{model.crop_w}+#{model.crop_h}"
  13.       img
  14.     end
  15.   end
  16.  
  17. end
  18.        
  19. process :cropper
  20.  
  21. def cropper
  22.   manipulate! do |img|
  23.     if model.crop_x.blank?
  24.       image = MiniMagick::Image.open(current_path)
  25.       model.crop_w = (image[:width] * 0.8).to_i
  26.       model.crop_y = (image[:height] * 0.8).to_i
  27.       model.crop_x = (image[:width] * 0.1).to_i
  28.       model.crop_y = (image[:height] * 0.1).to_i
  29.     end
  30.     img.crop "#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}"
  31.   end
  32. end