UPDATE I moved the crop code from the uploader directly to the Notebook Model so that it gets called at least. Now, the image is getting processed. It's getting cropped in fact, to the correct width and height dimensions, but the x and y offsets are wrong. I'm using MiniMagick, not RMagick. Here is my crop_image method:
def crop_image
if crop_x.present?
image.manipulate! do |img|
x = crop_x.to_i
y = crop_y.to_i
w = crop_w.to_i
h = crop_h.to_i
z = "#{w}x#{h}+#{x}+#{y}"
img.resize "600x600"
img.crop(z)
img
end
end
image.resize_to_fill(224, 150)
end
Has something changed in Rails 4 with regards to carrierwave or imagemagick? I copied over code that I had working (in a Rails 3 application) to upload an image, crop it, and save both versions, but I can't get it to work in the Rails 4 app.
Here are the relevant parts:
class Notebook < ActiveRecord::Base
validates :title, :access, presence: true
mount_uploader :image, ImageUploader
belongs_to :user
has_many :invites
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_image
def notebook_title
title
end
def user_name
user.name
end
def crop_image
image.recreate_versions! if crop_x.present?
end
end
And the recreate_versions! call accesses the image_uploader
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include Sprockets::Rails::Helper
storage :file
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :large do
process resize_to_limit: [600, 600]
end
version :thumb do
process :crop
resize_to_fill(224, 150)
end
def err
raise "error here"
end
def crop
if model.crop_x.present?
resize_to_limit(600, 600)
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
size = w << 'x' << h
offset = '+' << x << '+' << y
img.crop("#{size}#{offset}")
img
end
end
end
end
I'm seeing values for crop_x, crop_y, crop_w, and crop_h. The version :thumb block does not seem to call any process methods. Even if I try to raise an error from inside the version :thumb block, it doesn't work.
If anyone can provide any info that would be great.