Module: Drakkon::Images::Resize

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

Overview

General Image Index Helper

Class Method Summary collapse

Class Method Details

.imagesObject



73
74
75
# File 'lib/drakkon/lib/images/resize.rb', line 73

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

.process(file, filter, size) ⇒ Object



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

def self.process(file, filter, size)
  image = MiniMagick::Image.open(file)

  # Ignore if the same for w/h
  return if image.width == size.split('x', 2).first.to_i && image.height == size.split('x', 2).last.to_i

  LogBot.info('Image Resize', "Resizing: #{file}: #{size}")
  image.filter filter
  image.resize size
  image.write file
end

.promptObject



77
78
79
# File 'lib/drakkon/lib/images/resize.rb', line 77

def self.prompt
  TTY::Prompt.new(active_color: :cyan, interrupt: :exit)
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
# File 'lib/drakkon/lib/images/resize.rb', line 5

def self.run!(args = [])
  # Sizing
  size = if args.empty?
           puts "Use '#{'!'.pastel(:bright_blue)}' to force the size"
           prompt.ask('Target Size? (e.g. "200x200" or "64x64!"): ')
         else
           args.shift
         end

  # 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 images via MiniMagick and will force the size (breaking aspect ratio)
              - Sizes are #{'width'.pastel(:blue)} x #{'height'.pastel(:blue)}
                - 200x200, 150,175, and etc



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

    Filter: #{filter} to Size: #{size}
              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

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

  start(filter, size)
end

.start(filter, size) ⇒ Object



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

def self.start(filter, size)
  images.each do |img|
    LogBot.info('Image Resize', img)
    process(img, filter, size)
  end
end