【发布时间】:2026-01-18 04:00:01
【问题描述】:
我有一个带有回形针图像附件的内容表以及一些用户选择的裁剪设置:
create_table "content", force: true do |t|
t.string "title"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "crop_x"
t.integer "crop_y"
t.integer "crop_w"
t.integer "crop_h"
end
为了在服务器端处理用户指定的图像裁剪,我有一个自定义的回形针处理器,我从 here 采购:
module Paperclip
class CustomCropper < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@current_geometry.width = target.crop_w
@current_geometry.height = target.crop_h
end
def target
@attachment.instance
end
def transformation_command
crop_command = [
'-crop',
"#{target.crop_w}x" \
"#{target.crop_h}+" \
"#{target.crop_x}+" \
"#{target.crop_y}",
'+repage'
]
crop_command + super
end
end
end
我的问题是crop_x,y,w,h 参数是在图像附件之后解析的,因此自定义回形针处理器在所有字段中都看到nil 并且不能正确裁剪图像。
# rails processes the image_xxx params before the crop_xxx params
@content = Content.new(content_params)
有没有一种简洁的方法告诉 Rails 在附件图像之前处理裁剪边界字段? 或者是否有其他解决方案基本上可以完成此任务?
【问题讨论】:
标签: ruby-on-rails image paperclip