Class: Sequel::Plugins::Paperclip::Processors::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_paperclip/processors/image.rb,
lib/sequel_paperclip/processors/image.rb

Defined Under Namespace

Classes: Geometry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attachment, options) ⇒ Image

Returns a new instance of Image.



10
11
12
13
14
# File 'lib/sequel_paperclip/processors/image.rb', line 10

def initialize(attachment, options)
  @name = "image"
  @attachment = attachment
  @options = options
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



7
8
9
# File 'lib/sequel_paperclip/processors/image.rb', line 7

def attachment
  @attachment
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/sequel_paperclip/processors/image.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/sequel_paperclip/processors/image.rb', line 8

def options
  @options
end

Instance Method Details

#post_runsObject



56
57
# File 'lib/sequel_paperclip/processors/image.rb', line 56

def post_runs
end

#pre_runs(model, src_path) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/sequel_paperclip/processors/image.rb', line 16

def pre_runs(model, src_path)
  @model = model
  @src_path = src_path
  @src_geometry = Geometry.from_file(@src_path)
  raise ArgumentError, "failed to identify #{@src_path}" unless @src_geometry
end

#run(style, style_options, dst_file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequel_paperclip/processors/image.rb', line 23

def run(style, style_options, dst_file)
  dst_geometry = Geometry.from_s(style_options[:geometry])
  dst_crop = style_options[:geometry][-1,1] == "#"
  resize_str, crop_str = @src_geometry.transform(dst_geometry, dst_crop)

  cmd = []
  cmd << "convert"
  
  cmd << "-resize"
  cmd << "'#{resize_str}'"
  
  if crop_str
    cmd << "-crop"
    cmd << "'#{crop_str}'"
  end

  if options[:convert_arguments]
    cmd << options[:convert_arguments]
  end

  case style_options[:format]
    when :gif, :apng
      cmd << "'#{@src_path}'"
    else
      # extract first frame only (works even when input is non-animated)
      cmd << "'#{@src_path}[0]'"
  end

  cmd << "'#{style_options[:format]}:#{dst_file.path}'"

  `#{cmd*" "}`
end