Class: Rarity::Optimiser

Inherits:
Object
  • Object
show all
Defined in:
lib/rarity/optimiser.rb

Instance Method Summary collapse

Constructor Details

#initialize(tracker, options) ⇒ Optimiser

Returns a new instance of Optimiser.



2
3
4
5
# File 'lib/rarity/optimiser.rb', line 2

def initialize(tracker, options)
  @tracker = tracker
  @options = options
end

Instance Method Details

#optimise_image(path) ⇒ Object

Optimises a single image



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rarity/optimiser.rb', line 7

def optimise_image(path)
  # Tweak commands here if you want to. Defaults are needlessly overzealous and will take ages, especially PNG.
  png_cmd = "optipng -i0 -fix -o#{@options[:png_o_level]} -preserve"
  gif_cmd = "gifsicle -O3"
  jpg_cmd = "jpegoptim --max=95 -p"
  start = Time.now
  start_size = File.size(path)
  if @tracker.is_done?(path, @options)
    puts "Skipping image at #{path}, already done"
  else
    puts "Optimising image at #{path}, start filesize #{Rarity::to_human start_size}"
    # let's figure out what we've got
    ext = File.extname(path).downcase
    type = :unknown
    if ext == ".png"
      `#{png_cmd} "#{path}"`
      type = :png
      @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
    elsif ext == ".gif"
      type = :gif
      # ooh, okay, so if we're a gif, are we animated?
      eto = `exiftool "#{path}"`
      et = eto.split("\n")
      fc = et.detect{|l|l.include?("Frame Count")}
      if fc
        if (fc.split(":")[1].to_i rescue 1) > 0
          # We have more than one frame! We're animated or strange. gifsicle.
          `#{gif_cmd} "#{path}"`
          @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
        else
          # We're single frame, PNG probably does better
          opo = `#{png_cmd} "#{path}"`
          pngpath = path.gsub(File.extname(path),".png")
          if File.size(path) > File.size(pngpath)
            # We're done! Nuke the old file
            File.delete(path)
            # Changed format, so update path
            path = pngpath
            @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
          else
            # Clean up the PNG we tried and gifsicle it.
            File.delete(path.gsub(File.extname(path),".png"))
            `#{gif_cmd} "#{path}"`
            @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
          end
        end
      else
        # If we have no frame count data, assume not animated
        opo = `#{png_cmd} "#{path}"`
        pngpath = path.gsub(File.extname(path),".png")
        if File.size(path) > File.size(pngpath)
          # We're done! Nuke the old file
          File.delete(path)
          # Changed format, so update path
          path = pngpath
          @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
        else
          # Clean up the PNG we tried and gifsicle it.
          File.delete(path.gsub(File.extname(path),".png"))
          `#{gif_cmd} "#{path}"`
          @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
        end
      end
    elsif ext == ".jpg" or ext == ".jpeg"
      type = :jpg
      `#{jpg_cmd} "#{path}"`
      @tracker.mark_done(path, @options, {:before=>start_size, :after=>File.size(path), :filetype=>type, :time=>(Time.now-start)})
    else
      puts "Skipped file, not a recognised file type"
    end
  end
  return {:before=>start_size, :after=>File.size(path), :type=>type, :time=>(Time.now-start)}
end