Minimagick tends to operate inplace, so
screenshot.crop("#{w}x#{h}+#{x}+#{y}")
modifies screenshot rather than returning a new cropped image
Question
My screenshot model has a shot image attached via CarrierWave:
class Screenshot < ActiveRecord::Base
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
mount_uploader :shot, ShotUploader
process_in_background :shot unless Rails.env.test?
end
I have a form that gathers cropping information for the @screenshot#process_crop
action:
def process_crop
@survey = Survey.find(params[:survey_id])
@review = @survey.review
@screenshot = @survey.screenshots.find(params[:id])
@competitor = @survey.competitor
screenshot = MiniMagick::Image.open(@screenshot.shot.url)
if screenshot_params["crop_x"].present?
x = screenshot_params["crop_x"]
y = screenshot_params["crop_y"]
w = screenshot_params["crop_w"]
h = screenshot_params["crop_h"]
cropped_image = screenshot.crop("#{w}x#{h}+#{x}+#{y}")
uploader = ShotUploader.new
uploader.store!(cropped_image)
end
redirect_to survey_screenshot_path(@survey, @screenshot)
end
I have confirmed that x, y etc are sensible values given the dimensions of the image.
MiniMagick::Image.open(@screenshot.shot.url)
returns a MiniMagick image object as you'd expect.
But why does screenshot.crop("#{w}x#{h}+#{x}+#{y}")
return an empty string?
Clearly I need it to return a cropped image in a format that will make sense to uploader.store!
.
Solution
Minimagick tends to operate inplace, so
screenshot.crop("#{w}x#{h}+#{x}+#{y}")
modifies screenshot rather than returning a new cropped image