新手问题 Rails 4 Paperclip 3.0 对图片进行裁剪问题

dreamcity · 2013年10月06日 · 最后由 dreamcity 回复于 2013年10月07日 · 3821 次阅读

rails4 paperclip3.0 对上传的图片进行裁剪,似乎裁剪的数据没有被重新处理,因为结果还是原来的整张图。

问题描述:

  1. 上传图片,进入图片编辑界面。正常
  2. 对图片进行裁剪,crop_X/crop_y,crop_h,crop_w 坐标变化,显示裁剪缩略图。正常
  3. 完成裁剪,提交。出错

提交之后显示界面,图片仍然为整张图,没有被裁剪。

更新方法

def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
        if params[:user][:photo].blank?
          redirect_to @user
        else
          render :action => 'crop'
        end
    else    
        render 'edit'
    end
end
  private 
def user_params
      params.require(:user).permit(:name, :email, :password, :phone,
                                   :password_confirmation, :photo )
end

用户模型中:

has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" ,:large => "500x500>"}, 
                              :processors => [:cropper],
                              :url => "/assets/products/:id/:style/:basename.:extension",  
                              :path => ":rails_root/public/assets/products/:id/:styl/:basename.:extension"
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h  
    after_update :reprocess_photo, :if => :cropping?

我考虑到可能是因为我采用的是健壮参数的形式传递数据,难道这样 crop_x,crop_Y 等数据没有被传递过去?但是不确定。 这是处理裁剪动作

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
      end
    end
  end
end
1 楼 已删除

你可以在控制台看看,有木有裁剪参数啊!!

我考虑到可能是因为我采用的是健壮参数的形式传递数据,难道这样 crop_x,crop_Y 等数据没有被传递过去?但是不确定

你得自己确定呀

#2 楼 @shatle 有裁剪参数啊,但没传过去,因为裁剪后还是原图!

#3 楼 @hooopo 是这样,因为我对值的传递还是理解不够!比如说,我对用户信息更改,似乎只在 new.html.erb 和 edit.html.erb 中成功,其它的页面,更改之后提交,数据库完全没变化!是不是与控制器中的方法有关?

.permit(:name, :email, :password, :phone, :password_confirmation, :photo )

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

你的这四个值没传进去,cropping 方法可能就 false 了吧。试试下面: .permit(:name, :email, :password, :phone, :password_confirmation, :photo,:crop_x, :crop_y, :crop_w, :crop_h )

#6 楼 @lmorenbit 非常感谢,问题解决了部分!现在是裁剪实现了,但是服务器会崩溃.....如果还是将这四个参数传过去的话,重启之后还是崩!只有删掉,会正常。而且图片裁剪爱成功了!

#6 楼 @lmorenbit 刚学两个多月,我再鼓捣鼓捣!!

#6 楼 @lmorenbit 服务器就不定的处理这个问题,重复的!

User Exists (0.4ms)  SELECT 1 AS one FROM `users` WHERE (`users`.`email` = '[email protected]' AND `users`.`id` != 8) LIMIT 1
  SQL (0.3ms)  UPDATE `users` SET `photo_updated_at` = '2013-10-07 02:53:52', `updated_at` = '2013-10-07 02:53:52', `photo_file_name` = 'nightmash.jpg' WHERE `users`.`id` = 8
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "300x300>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-faee8k'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-faee8k'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "100x100>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-td1rv4'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-td1rv4'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: identify -format %m '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]'
Command :: convert '/tmp/nightmash20131007-11429-10t8ohw.jpg[0]' -crop 295x295+200+115 -auto-orient -resize "500x500>" '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-9vm6w8'
Command :: file -b --mime '/tmp/nightmash20131007-11429-10t8ohw20131007-11429-9vm6w8'
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/original/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/medium/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/thumb/nightmash.jpg
[paperclip] deleting /home/dreamcity/rails_project/flowerstore/public/assets/products/8/large/nightmash.jpg
  User Exists (0.3ms)  SELECT 1 AS one FROM `users` WHERE (`users`.`email` = '[email protected]' AND `users`.`id` != 8) LIMIT 1
  SQL (0.3ms)  UPDATE `users` SET `photo_updated_at` = '2013-10-07 02:53:53', `updated_at` = '2013-10-07 02:53:52', `photo_file_name` = 'nightmash.jpg' WHERE `users`.`id` = 8

#9 楼 @dreamcity 抱歉,你给的这些信息我看不出什么来。

#10 楼 @lmorenbit 还是非常感谢你的帮助!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号