更新 我将裁剪代码从上传器直接移动到Notebook Model,以便至少调用它。现在,图像正在处理中。它实际上被裁剪到正确的宽度和高度尺寸,但x和y偏移是错误的。我正在使用MiniMagick,而不是RMagick。这是我的crop_image方法:
def crop_image
if crop_x.present?
image.manipulate! do |img|
x = crop_x.to_i
y = crop_y.to_i
w = crop_w.to_i
h = crop_h.to_i
z = "#{w}x#{h}+#{x}+#{y}"
img.resize "600x600"
img.crop(z)
img
end
end
image.resize_to_fill(224, 150)
end
Rails 4中有关于carrierwave或imagemagick的更改吗?我复制了我曾经工作的代码(在Rails 3应用程序中)来上传图像,裁剪它并保存两个版本,但我无法在Rails 4应用程序中使用它。
以下是相关部分:
class Notebook < ActiveRecord::Base
validates :title, :access, presence: true
mount_uploader :image, ImageUploader
belongs_to :user
has_many :invites
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_image
def notebook_title
title
end
def user_name
user.name
end
def crop_image
image.recreate_versions! if crop_x.present?
end
end
还有recreate_versions! call访问image_uploader
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include Sprockets::Rails::Helper
storage :file
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :large do
process resize_to_limit: [600, 600]
end
version :thumb do
process :crop
resize_to_fill(224, 150)
end
def err
raise "error here"
end
def crop
if model.crop_x.present?
resize_to_limit(600, 600)
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
size = w << 'x' << h
offset = '+' << x << '+' << y
img.crop("#{size}#{offset}")
img
end
end
end
end
我看到了crop_x,crop_y,crop_w和crop_h的值。版本:thumb块似乎没有调用任何进程方法。即使我尝试从版本:拇指块内部引发错误,它也不起作用。
如果有人可以提供任何有用的信息。