Class: Alula::Magick

Inherits:
ImageProcessor show all
Defined in:
lib/alula/processors/magick.rb

Instance Attribute Summary

Attributes inherited from Processor

#attachments, #item, #options, #site

Instance Method Summary collapse

Methods inherited from ImageProcessor

#process, #sizes

Methods inherited from Processor

#asset_path, available?, #initialize, mimetype, #process, process?

Constructor Details

This class inherits a constructor from Alula::Processor

Instance Method Details

#cleanupObject



38
39
40
41
42
43
44
# File 'lib/alula/processors/magick.rb', line 38

def cleanup
  super
  
  @info = nil
  @image = nil
  @exif = nil
end

#exifObject



56
57
58
# File 'lib/alula/processors/magick.rb', line 56

def exif
  @exif ||= MiniExiftool.new self.item.filepath
end

#imageObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/alula/processors/magick.rb', line 60

def image
  @image ||= begin
    image = ::Magick::Image.read(self.item.filepath).first
    unless self.options[:no_rotate]
      case image.orientation.to_i
        when 2
          image.flop!
        when 3
          image.rotate!(180)
        when 4
          image.flip!
        when 5
          image.transpose!
        when 6
          image.rotate!(90)
        when 7
          image.transverse!
        when 8
          image.rotate!(270)
      end
      
      image
    end
  end
end

#infoObject



46
47
48
49
50
51
52
53
54
# File 'lib/alula/processors/magick.rb', line 46

def info
  @info ||= begin
    info = ::Magick::Image.ping(self.item.filepath).first
    Hashie::Mash.new({
      width: info.columns,
      height: info.rows,
    })
  end
end

#resize_image(opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alula/processors/magick.rb', line 11

def resize_image(opts)
  output = opts[:output]
  width, height = opts[:size]
  
  # Generate resized image
  resize_mode = self.site.config.attachments.image["#{opts[:type]}_mode"]
  resized = case resize_mode
  when :square
    self.image.resize_to_fill(width, height)
  else
    self.image.resize_to_fit(width, height)
  end
  # Make it progressive
  resized.interlace = ::Magick::PlaneInterlace
  
  # Strip unwanted properties
  tags = Hash[*(self.site.config.attachments["image"]["keep_tags"].collect{|t| [t, self.exif[t]]}).flatten]
  resized.strip!
  
  resized.write(output)
  
  # Save our EXIF info
  exif = MiniExiftool.new output
  tags.each {|key, value| exif[key] = value }
  exif.save
end