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



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

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

.process(file, filter, size) ⇒ Object



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

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



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

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
# File 'lib/drakkon/lib/images/resize.rb', line 5

def self.run!(args = [])
  # Sizing
  size = if args.empty?
           prompt.ask('Target Size? (e.g. 200x200): ')
         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 the images via MiniMagick
              - 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



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

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