为什么MiniMagick的裁剪方法返回一个空字符串?

时间:2014-04-18 01:14:42

标签: ruby-on-rails imagemagick carrierwave

我的屏幕截图模型有一个通过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

我有一个表单,可以收集@screenshot#process_crop操作的裁剪信息:

  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

我已经确认x,y等是给定图像尺寸的合理值。

MiniMagick::Image.open(@screenshot.shot.url)会像您期望的那样返回一个MiniMagick图像对象。

但为什么screenshot.crop("#{w}x#{h}+#{x}+#{y}")会返回一个空字符串?

显然,我需要它以一种对uploader.store!有意义的格式返回裁剪的图像。

1 个答案:

答案 0 :(得分:0)

Minimagick倾向于在原地运作,所以

screenshot.crop("#{w}x#{h}+#{x}+#{y}")

修改屏幕截图而不是返回新的裁剪图像