使用回形针3.3.1在轨道中进行图像裁剪

时间:2012-12-07 09:40:25

标签: ruby-on-rails image-processing paperclip

我已经按照裁剪教程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查询导致问题,并且正在更新图像并在循环中将其删除,就像我在控制台输出中看到的那样。

模块中的回形针处理器似乎正在造成这种情况。但我不知道为什么。

OR

这是User模型中的内容 -

class User<的ActiveRecord ::基

  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 

1 个答案:

答案 0 :(得分:1)

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