在 rails 中使用回形针 3.3.1 进行图像裁剪
Posted
技术标签:
【中文标题】在 rails 中使用回形针 3.3.1 进行图像裁剪【英文标题】:Image cropping with paperclip 3.3.1 in rails 【发布时间】:2012-12-07 09:40:25 【问题描述】:我遵循了裁剪教程http://asciicasts.com/episodes/182-cropping-images。 一切正常,图像上传成功,但裁剪不起作用。当我选择要裁剪的某个区域然后单击裁剪按钮时,回形针裁剪头像然后将其删除。这个过程在一个永无止境的循环中重复。结果我强行停止我的服务器来停止它。
我的控制台输出:
Started POST "/users/change_img" for 127.0.0.1 at 2012-12-07 14:59:29 +0530
Processing by UsersController#change_img as html
Parameters: "utf8"=>"✓", "authenticity_token"=>"k5fMemUGLIgbpIjiguRQKL9eiZ=",
"user"=>"crop_x"=>"230", "crop_y"=>"0", "crop_w"=>"249", "crop_h"=>"249",
"commit"=>"Crop"
[1m[35mUser Load (0.4ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 36 LIMIT 1
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-1y5h619.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "x100" +repage '/tmp/freedomking20121207-23324-1y5h61920121207-23324-xzdzn7'
Command :: file -b --mime '/tmp/freedomking20121207-23324-1y5h61920121207-23324-xzdzn7'
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1y5h619.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-1y5h619.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "500x500>" '/tmp/freedomking20121207-23324-1y5h61920121207-23324-z3d34f'
Command :: file -b --mime '/tmp/freedomking20121207-23324-1y5h61920121207-23324-z3d34f'
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper /public/system/avatars/36/original/freedomking.jpg
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper/public/system/avatars/36/small/freedomking.jpg
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper/public/system/avatars/36/large/freedomking.jpg
[1m[35m (0.7ms)[0m UPDATE "users" SET "avatar_updated_at" = '2012-12-07 09:29:29.286191', "updated_at" = '2012-12-07 09:29:29.639348' WHERE "users"."id" = 36
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-1nreivu.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "x100" +repage '/tmp/freedomking20121207-23324-1nreivu20121207-23324-1l0osan'
Command :: file -b --mime '/tmp/freedomking20121207-23324-1nreivu20121207-23324-1l0osan'
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-1nreivu.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-1nreivu.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "500x500>" '/tmp/freedomking20121207-23324-1nreivu20121207-23324-1irzeiq'
Command :: file -b --mime '/tmp/freedomking20121207-23324-1nreivu20121207-23324-1irzeiq'
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper/public/system/avatars/36/original/freedomking.jpg
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper/public/system/avatars/36/small/freedomking.jpg
[paperclip] deleting /home/saurabh/Workspaces/eclipse/img_cropper_ws/paperclip_cropper/public/system/avatars/36/large/freedomking.jpg
[1m[36m (0.3ms)[0m [1mUPDATE "users" SET "avatar_updated_at" = '2012-12-07 09:29:29.648417', "updated_at" = '2012-12-07 09:29:29.639348' WHERE "users"."id" = 36[0m
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "x100" +repage '/tmp/freedomking20121207-23324-f9rq0q20121207-23324-1r94x6v'
Command :: file -b --mime '/tmp/freedomking20121207-23324-f9rq0q20121207-23324-1r94x6v'
Command :: identify -format %wx%h '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: identify -format %m '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]'
Command :: convert '/tmp/freedomking20121207-23324-f9rq0q.jpg[0]' -crop 249x249+230+0 -auto-orient -resize "500x500>" '/tmp/freedomking20121207-23324-f9rq0q20121207-23324-m0brtm'
Command :: file -b --mime '/tmp/freedomking20121207-23324-f9rq0q20121207-23324-m0brtm'
如您所见,在控制台中有重复的Update
查询,并且永不结束。
还要注意,教程中提到的module Paperclip
不起作用,所以我做了一些更改以使其起作用。虽然它有效但导致了这种尴尬的问题。
当我在强制终止服务器后检查图像时,我看到图像在文件夹中被成功裁剪。但随后也会在循环中删除并重复此操作。
我的裁剪方法
def change_img
@user = current_user
@user.update_attributes(params[:user])
flash[:notice] = "Successfully updated Image."
render :action => 'crop'
end
在上述方法中,update attribute
查询会导致问题,并在循环中更新图像并删除它,正如您在我的控制台输出中看到的那样。
似乎是模块中的回形针处理器导致了这种情况。但我不知道为什么。
或
这是User
模型中的东西吗 -
类用户<:base>
has_attached_file :avatar, :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:processors => [:cropper],
:styles => :small => "100x100#", :large => "500x500>"
after_update :reprocess_avatar, :if => :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
def avatar_geometry(style = :original)
@geometry ||=
@geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
private
def reprocess_avatar
avatar.reprocess!
end
结束
【问题讨论】:
我发现了问题。它在用户模型中。实际上,在更新用户之后,裁剪方法被一次又一次地调用。但我不知道如何避免这种延续。如果我删除after_update
中的 if 条件,则每次更新用户时都会调用裁剪方法。
【参考方案1】:
调用 avatar.reprocess!来自控制器以避免递归。
【讨论】:
以上是关于在 rails 中使用回形针 3.3.1 进行图像裁剪的主要内容,如果未能解决你的问题,请参考以下文章