【发布时间】:2015-02-06 17:07:13
【问题描述】:
我正在使用 Rails 4.2 和 Mongoid 构建一个网站。我正在使用mongoid-paperclip,我正在尝试将图像裁剪为正方形,同时保留短边的尺寸(因此图像将填充整个正方形)。这是我的自定义处理器:
module Paperclip
class Cropper < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@preserved_size = [@current_geometry.width, @current_geometry.height].min
@current_geometry.width = @preserved_size
@current_geometry.height = @preserved_size
end
def target
@attachment.instance
end
def transformation_command
if crop_command
crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
else
super
end
end
def crop_command
["-crop", "#{@preserved_size}x#{@preserved_size}+#{@preserved_size}+#{@preserved_size}"]
end
end
end
并且它所附加的模型看起来有这样一行:
has_mongoid_attached_file :image, styles: {square: {processors: [:cropper]}}
但它似乎不起作用。保存了名为“square”的图像版本,但它与原始图像相同。我怎样才能让它工作?
【问题讨论】:
标签: ruby-on-rails image ruby-on-rails-4 paperclip minimagick