Module: Drakkon::Images::Scale

Defined in:
lib/drakkon/lib/images/scale.rb

Overview

General Image Index Helper

Class Method Summary collapse

Class Method Details

.imagesObject



75
76
77
# File 'lib/drakkon/lib/images/scale.rb', line 75

def self.images
  Dir["#{Dir.pwd}/**/*.png"]
end

.promptObject



79
80
81
# File 'lib/drakkon/lib/images/scale.rb', line 79

def self.prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
end

.resize(file, filter, scale) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drakkon/lib/images/scale.rb', line 61

def self.resize(file, filter, scale)
  image = MiniMagick::Image.open(file)

  # Calculate Size / Width Primpt
  width = (image.width * scale).round
  height = (image.height * scale).round
  size = "#{width}x#{height}"

  LogBot.info('Image Scale', "#{File.basename file} => #{scale}: #{size}")
  image.filter filter
  image.resize size
  image.write file
end

.run!(args = []) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/drakkon/lib/images/scale.rb', line 5

def self.run!(args = [])
  # Sizing
  scale = if args.empty?
            prompt.ask('Target Scale? (e.g. 100,50,25): ')
          else
            args.shift
          end

  scale = scale.to_f / 100
  LogBot.info('Image Scale', "Scale: #{scale}")

  # Recommend as Options?
  # image.filter 'Sinc'
  # image.filter 'Gaussian'

  # Potentially Support Custom Filters?
  filter = if args.empty?
             'Lanczos2'
           else
             args.shift
           end

  puts <<~HELP
              Usage [size] [filter = Lanczos2]
              - Run in the directory you wish to modify the images
              - Resizes the images via MiniMagick
              - Each image sizing values scaled by (value / 100)

    #{'Note: this will be best fit! This will retain aspect ratio!'.pastel(:bright_blue)}

    Filter: #{filter} to Size: #{scale}
              Current Directory;
                #{Dir.pwd.pastel(:yellow)}

    Images:
  HELP

  images.sort.each do |img|
    img_s = img.gsub("#{Dir.pwd}/", '').pastel(:yellow)
    puts "  - #{img_s}"
  end
  puts

  unless prompt.yes? "#{'Are you sure?'.pastel(:bright_yellow)} Scale Images!? #{'(Destructive)'.pastel(:red)}"
    exit
  end

  start(filter, scale)
end

.start(filter, scale) ⇒ Object



55
56
57
58
59
# File 'lib/drakkon/lib/images/scale.rb', line 55

def self.start(filter, scale)
  images.each do |img|
    resize(img, filter, scale)
  end
end