SHARE
TWEET

Carrierwave Cropping

a guest Apr 9th, 2012 183 Never
  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
RAW Paste Data
Top