有没有办法将新尺寸设为my_image
并将其保存到AWS S3
?
我有
my_image = Post.last.photo.image
geometry = Paperclip::Geometry.from_file(my_image.url)
所以我想设置新几何并保存它
geometry = 'my params'
my_image.save
当然我有我的
has_attached_file :image, :styles => { :large => "220x" ..other styles}
我的主要目标是在使用Jcrop
所以当我收到my_params
=> "crop_x"=>"83", "crop_y"=>"24", "crop_w"=>"76", "crop_h"=>"76"
这样的新参数时,将其设置为has_attached_file
答案 0 :(得分:1)
你可以轻松地做到这一点,
为裁剪尺寸
声明属性访问器attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
然后您需要覆盖处理器,在cropper.rb
中创建/lib/paperclip_processors
并添加以下代码
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
else
super
end
end
def crop_command
target = @attachment.instance
if target.cropping?
["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
end
end
end
end
现在在模型中你可以做到这一点
has_attached_file :image, styles: { large: "800X800>" }, default_url: "/images/:style/missing.png", :processors => [:cropper]
after_save :reprocess_image, if: :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
def reprocess_image
image.assign(image)
image.save
end
希望这有帮助!