1

我遵循了裁剪教程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-

类用户 < ActiveRecord::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 

结尾

4

1 回答 1

1

调用 avatar.reprocess!来自控制器以避免递归。

于 2013-07-16T20:38:38.723 回答