【发布时间】:2011-12-28 07:33:29
【问题描述】:
我在使用回形针和自定义处理器时遇到了一个棘手的问题,我丢失了一些样式。
我有一张图片,我使用从视图传递的坐标进行裁剪。 裁剪进行得很顺利,原始图片已保存,但在运行重新处理以制作其他样式时出现此错误:
convert: geometry does not contain image `/var/folders/bg/w2mft0x51t933pzhgpvhm5800000gn/T/paperclip-reprocess20111114-3404-13lp780-0' @ warning/transform.c/CropImage/571.
当我收到此错误原始图像样式丢失。
我试着玩弄回调和回形针的东西,但没有成功。我真的被困住了,找不到任何关于此事的信息。
我遵循的例子来自http://railscasts.com/episodes/182-cropping-images
与截屏视频中的示例不同的是,我想裁剪原始文件,然后使用标准回形针样式从中生成缩略图。
这是我的代码。
我的处理器:
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
original_command = super
if original_command.include?('-crop')
original_command.delete_at(super.index('-crop') + 1)
original_command.delete_at(super.index('-crop'))
end
crop_command + original_command
else
super
end
end
def crop_command
target = @attachment.instance
if target.cropping?
["-crop", "#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}", "+repage"]
end
end
end
end
我的模特:
class Item < ActiveRecord::Base
belongs_to :category
belongs_to :picture
validates :picture_id, :presence => true
validates :category_id, :presence => true
validates :title_url, :presence => true, :uniqueness => { :case_sensitive => false }
validates :title, :presence => true, :uniqueness => { :case_sensitive => false }
validates :information, :presence => true, :uniqueness => { :case_sensitive => false }
validates :crop_x, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :crop_y, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :crop_w, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :crop_h, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
Paperclip.interpolates :title_url do |attachment, style|
attachment.instance.title_url
end
has_attached_file :image,
:styles => {
:small => { :format => 'jpg', :quality => 100, :geometry => '100x100#' },
:medium => { :format => 'jpg', :quality => 100, :geometry => '200x200#' },
:large => { :format => 'jpg', :quality => 100, :geometry => '300x300#' },
:original => { :format => 'jpg', :quality => 100, :geometry => '', :processors => [:cropper] }
},
:path => ":rails_root/public/attachments/:class/:attachment/:id_partition/:style/:title_url.:extension",
:url => "/attachments/:class/:attachment/:id_partition/:style/:title_url.:extension"
before_validation :strip_attributes
before_save :image_assign, :if => :cropping?
after_create :image_reprocess, :if => :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
def image_geometry(style = :original)
@geometry ||= {}
@geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
def calc(width,height)
# original large side / smalled picture large side
calc_width = self.picture.width.to_f / width
calc_height = self.picture.height.to_f / height
if calc_width >= calc_height
return calc_width
else
return calc_height
end
end
private
def image_assign
self.image = self.picture.image
end
def image_reprocess
image.reprocess!
end
def strip_attributes
# normalize attributes
self.title.strip!
self.title_url = Utility::friendly_url(self.title)
self.information.strip!
end
end
【问题讨论】:
-
我会从 :image => :styles => :original... 中取出 ":geometry => ''" ...保存。嗯
-
没有几何图形,即使是基本设置也会失败。定义样式时似乎需要它。
-
我应该澄清(如果上面不清楚的话)重新处理会破坏原始图像。在调用重新处理之前保存似乎很好。
-
对我来说,是 before_validation 导致回形针出现问题。我一直没有得到 nil 类的方法大小>.
标签: ruby-on-rails ruby imagemagick paperclip