我在这里已经有一段时间了。使用这个RailsCast,我能够修改它以使用CarrierWave --至少在理论上是这样的。我试图让用户裁剪他们的个人资料照片,然后上传到S3使用CarrierWave。以下是到目前为止所起的作用:
这是我的密码。下面是模型:
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :original_width, :original_height
attr_accessible :avatar, :remove_avatar
after_update :reprocess_avatar, :if => :cropping?
mount_uploader :avatar, ProfileBaseUploader
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
private
def reprocess_avatar
avatar.process!
avatar.recreate_versions!
end漂亮的锅炉板- crop_x等属性是从作物视图分配的。我已经确认这些被传入并被正确分配,并且reprocess_avatar方法正在被调用。
这是我的上传代码:
include CarrierWave::MiniMagick
include CarrierWaveDirect::Uploader
storage :fog
require 'guid'
process :cropper
process :store_best_geometry
version :tiny_thumb do
process :resize_to_limit => [50, 50]
end
version :thumb do
process :resize_to_limit => [200, 200]
end
version :large do
process :resize_to_fit => [500, 500]
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
@name ||= "#{secure_token}.#{file.extension}" if original_filename.present?
end
def store_best_geometry
manipulate! do |img|
if model
model.original_width = img['width']
model.original_height = img['height']
end
img = yield(img) if block_given?
img
end
end
def cropper
return unless model.cropping?
manipulate! do |img|
img = img.crop("#{model.crop_x}x#{model.crop_y}+#{model.crop_w}+#{model.crop_h}")
img
end
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, Guid.new)
end我认为的过程!还有recreate_versions!方法根本不能正常工作,但我不知道为什么。我没有任何可以提供的错误(如果有人知道我如何产生错误,我很乐意这样做)。我知道store_best_geometry方法工作得很好。不能对种植者说同样的话。
有什么想法吗?
发布于 2012-05-29 02:51:35
就像一次严重的空难,我做了很多错误的事情。
首先,我的论点是作物的顺序不对。在我的问题中,你会注意到我有:
img.crop('[x offset]x[y offset]+[width]+[height]')正确的顺序是:
img.crop('[width]x[height]+[x offset]+[y offset]')所以这是第一个问题。试图以错误的顺序执行该操作是在抛出一个错误(深入控制台以查找该错误),说明该几何图形被提供为无效。
下一个问题:用我的裁剪方法打破一个屈服链,返回一个字符串而不是一个图像。结果是,裁剪方法返回一个字符串。看一下原来的收割机方法:
def cropper
return unless model.cropping?
manipulate! do |img|
img = img.crop("#{model.crop_x}x#{model.crop_y}+#{model.crop_w}+#{model.crop_h}")
img
end
end糟了!返回的最后一件事是字符串。结果,正确的方法是直接调用图像上的方法:
def cropper
return unless model.cropping?
manipulate! do |img|
img.crop("#{model.crop_w}x#{model.crop_h}+#{model.crop_x}+#{model.crop_y}")
img = yield(img) if block_given?
img
end
end我做错的最后一件事--这完全是业余时间的错误--并没有让一些关键属性可以访问。在我的无限智慧中,我假设attr_accessor将使属性可访问。不是的。我必须修改我的模型,以使crop_x、crop_y、crop_w和crop_h参数可访问。
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :original_width, :original_height
attr_accessible :avatar, :remove_avatar, :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_avatar, :if => :cropping?最后一个音符,不是关键,但很容易知道。我不需要打电话给进程!作为recreate_versions!为我这么做。
希望这能帮到至少一个人。
https://stackoverflow.com/questions/10791554
复制相似问题