conditional convert options paperclip

后端 未结 5 1442
[愿得一人]
[愿得一人] 2021-02-06 09:18

After some research I was able to add styles based on my image_class column.

Model.rb

has_attached_file :image,
                    


        
5条回答
  •  臣服心动
    2021-02-06 10:04

    note: haven't verified that on working code

    It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an attachment)

    Try:

    convert_options: lambda { |instance| instance.decide_convert_options }
    

    Btw your code would look much better if you extract the configuration data, for example:

    has_attached_file :image,
      styles: lambda { |attachment| attachment.instance.image_options[:styles] }, 
      convert_options: lambda { |instance| instance.image_options[:convert_options] }
    
    
    IMAGE_OPTIONS = {
      poster: {
        styles: {
          thumb: ["30x45!", :jpg],
          standard: ["185x278!", :jpg],
          expanded: ["372x559!", :jpg]
          big: ["600x900!", :jpg]
        },
        convert_options: {
          thumb: "-flop",
          standard: "-flop",
          expanded: "-flop",
          big: = "-flop"
        }
      },
      cover: {
        styles: {
          thumb: ["30x45!", :jpg],
          standard: ["300x1200!", :jpg]
        },
        convert_options: {
          thumb: "-enhance",
          standard: "-enhance"
        }
      }
    }
    
    def image_options
      IMAGE_OPTIONS[self.image_class]
    end
    

    I hope that helps

    Update:

    it looks like your convert_options are not being set here: https://github.com/thoughtbot/paperclip/blob/a93dfc773b4fd649db4d1281b42a2a71b1ae72ff/lib/paperclip/style.rb#L55

    it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41

    can you try this? so remove convert_options entirely, and in your configuration return hash like:

    IMAGE_OPTIONS = {
          poster: {
            styles: {
              thumb: {
                geometry: "30x45!",
                format: :jpg,
                convert_options: '-flop',
              },
              standard: {...}
              expanded: {...}
              big: {...}
            }
          },
          cover: {
            styles: {...}
    

提交回复
热议问题