【问题标题】:get Jcrop coordinates to convert_options in Paperclip and Rails 4在 Paperclip 和 Rails 4 中将 Jcrop 坐标转换为 convert_options
【发布时间】:2016-02-03 03:32:15
【问题描述】:

我正在尝试从 Jcrop 获取用户隐藏字段参数并在将图像放入数据库之前对其进行裁剪。我找到了一些教程和问题,但没有一个能解决我的确切问题。

这是最接近的。看起来海报试图使用选择中的单个参数。我有 4 个整数。我认为这无关紧要,所以我认为这是“has_attached_file”部分:

Supplying a variable string to model for Paperclip Imagemagik resizing

结合这个:

https://github.com/thoughtbot/paperclip#dynamic-configuration

这就是我所拥有的,

型号:

class Muse < ActiveRecord::Base
attr_accessor :w, :h, :x, :y

def get_coor 

 #turn parameters into class variables
  @w = self.w
  @h = self.h
  @x = self.x
  @y = self.y

  #build string for options
  cropper = "-crop #{@w.to_i}x#{@h.to_i}+#{@x.to_i}+#{@y.to_i} -resize 200x200"

  #return string of the convert options I want
  @get_coor = cropper

end

belongs_to :user
default_scope -> { order(created_at: :desc) }

has_attached_file :muse_img, styles: lambda { |attachment| { original: (attachment.instance.get_coor) } }

validates_attachment :muse_img, presence: true,
                 content_type: { content_type: ["image/jpeg"] },
                 size: { in: 0..500.kilobytes }

end

我没有收到错误。但它的行为就像 get_coor 方法没有调用任何东西。

在此之前,我直接在 convert_options 上尝试了字符串插值,但对于所有坐标,它始终显示为 nil。我是 Rails 和 Ruby 的新手,所以我对 lambdas 还是有些模糊。希望这只是一个语法错误。感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby lambda paperclip jcrop


    【解决方案1】:

    解决方案似乎是我将参数放入 params 方法的顺序。我必须先放坐标,然后再放图像。我在这里找到了它:

    https://github.com/thoughtbot/paperclip/issues/1533

    我还更改了 has_attached_file 部分,但我不知道这是否与问题有关。但无论如何,它就在这里。

    has_attached_file :muse_img,
    :styles => lambda { |attachment| 
    
        crop_convert_options = attachment.instance.get_coor
    
        {
            original: {
                convert_options: crop_convert_options
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 2015-03-07
      • 2014-03-05
      • 2014-06-12
      • 2016-12-18
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多