设计+ Carrierwave裁剪图像

时间:2017-07-23 22:02:38

标签: ruby-on-rails devise carrierwave crop

我尝试使用裁剪后的图片更新现有用户头像。控制器也获得所有需要的参数(avatar_crop_x,avatar_crop_y,avatar_crop_w,avatar_crop_h)。我已经devise_parameter_sanitizer.permit(:account_update)方法允许使用它们。

Started PATCH "/users" for 127.0.0.1 at 2017-07-23 23:50:29 +0300
Processing by Devise::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"n8ZQYS5iZXJs6V5eS4PfJKDeUhugasvXXIvIP+ZZIyLV4tukuqqjSvUA
+DnZFZSF468eam1vSF8hD5STYmeA8Q==", "user"=>{"email"=>"admin@example.com", "username"=>"admin", "first_name"=>"",
                                            "last_name"=>"", "position"=>"", "city"=>"", "bio"=>"",
                                            "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f82cb8f3860
# @tempfile=#<Tempfile:/var/folders/2h/4k0zwm8s4j1dfq5_8zf7ttmr0000gn/T/RackMultipart20170723-11835-fia4di.jpg>,
# @original_filename="gloomy-mountain-lake-hd-wallpaper.jpg", @content_type="image/jpeg",
# @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"gloomy-mountain-lake-hd-wallpaper
# .jpg\"\r\nContent-Type: image/jpeg\r\n">, "avatar_crop_x"=>"1266.6666666666665", "avatar_crop_y"=>"319
# .9999999999999", "avatar_crop_w"=>"1280", "avatar_crop_h"=>"1280", "password"=>"[FILTERED]",
# "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}

此外,我在用户模型中有四个字段。

class User 
...
attr_accessor :avatar_crop_x, :avatar_crop_y, :avatar_crop_w, :avatar_crop_h
...

但是在我的上传器中,所有model.avatar_crop_*字段都是零。

class AvatarUploader < BaseUploader
  process :crop

  version :thumb do
    process resize_to_fill: [50, 50]
  end

  def crop
    if model.avatar_crop_x.present?
      manipulate! do |img|
        w = model.avatar_crop_w.to_i
        h = model.avatar_crop_h.to_i

        # Set x-y coordinates of cropped image.
        x = model.avatar_crop_x.to_i
        y = model.avatar_crop_y.to_i
        img.crop "#{w}x#{h}+#{x}+#{y}"
      end
    end
  end
end

有人有同样的问题吗?请帮我。感谢

1 个答案:

答案 0 :(得分:0)

确保在设置avatar之前设置了裁剪参数。

例如:

这并不能在上传器中提供高度:

User.new(avatar: file, height: "10px")

这样做:

User.new(height: "10px", avatar: file)

检查允许参数的订单。