Class: Alula::ImageProcessor

Inherits:
Processor show all
Defined in:
lib/alula/processors/image.rb

Direct Known Subclasses

Magick

Instance Attribute Summary

Attributes inherited from Processor

#attachments, #item, #options, #site

Instance Method Summary collapse

Methods inherited from Processor

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

Constructor Details

This class inherits a constructor from Alula::Processor

Instance Method Details

#processObject



3
4
5
6
7
8
9
10
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/image.rb', line 3

def process
  super
  
  sizes.each do |size|
    width, height = size[:size]
    # output_dir = self.site.storage.path(:cache, "attachments", size[:type].to_s)
    
    # Generate attachment hash
    name = File.join size[:type].to_s, if size[:hires]
      ext = File.extname(item.name)
      item.name.gsub(/#{ext}$/, "-hires#{ext}")
    else
      item.name
    end
    
    output = asset_path(name, size[:type].to_s)
    # asset_name = self.attachments.asset_name(name, size[:type].to_s)
    # output = File.join(self.site.storage.path(:cache, "attachments"), asset_name)
    # # Make sure our directory exists
    # output_dir = self.site.storage.path(:cache, "attachments", File.dirname(asset_name))
    
    # Skip image processing if output already exists...
    next if File.exists?(output)
    
    # Skip if our original resolution isn't enough for hires images
    next if (width > self.info.width and height > self.info.height) and size[:hires]
    
    # Generate resized image to output path
    resize_image(output: output, size: size[:size], type: size[:type])
  end
  
  # Cleanup ourself
  cleanup
end

#sizesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alula/processors/image.rb', line 38

def sizes
  @sizes ||= begin
    sizes = []
    sizes << { type: :image,  hires: false, size: self.site.config.attachments["image"]["size"].split("x").collect{|i| i.to_i} }
    sizes << { type: :thumbnail, hires: false, size: self.site.config.attachments["image"]["thumbnail"].split("x").collect{|i| i.to_i} }
    
    if self.site.config.attachments["image"]["hires"]
      sizes += sizes.collect {|s| { type: s[:type], hires: true, size: s[:size].collect{|x| x*2} } }
    end
    
    sizes
  end
end