Module: Drakkon::Images::Compress

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

Overview

Dirty Compression.. Bad Idea?

Class Method Summary collapse

Class Method Details

.imagesObject



49
50
51
# File 'lib/drakkon/lib/images/compress.rb', line 49

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

.process(file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/drakkon/lib/images/compress.rb', line 31

def self.process(file)
  basename = File.basename(file, '.png')
  path = File.dirname(file)

  # Write Webp
  image = MiniMagick::Image.open(file)
  image.format 'webp'
  image.write "#{path}/#{basename}.webp"

  # Write back to PNG
  image = MiniMagick::Image.open("#{path}/#{basename}.webp")
  image.format 'png'
  image.write "#{path}/#{basename}.png"

  # Delete Temp
  FileUtils.rm("#{path}/#{basename}.webp")
end

.promptObject



53
54
55
# File 'lib/drakkon/lib/images/compress.rb', line 53

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

def self.run!(_args = [])
  puts <<~HELP
              Usage
              - Run in the directory you wish to modify the images
              - Converts the Images to Webp and then back to PNG
    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)} Compress!? #{'(Destructive)'.pastel(:red)}"

  start
end

.startObject



24
25
26
27
28
29
# File 'lib/drakkon/lib/images/compress.rb', line 24

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