Class: ImageOptimizer

Inherits:
Synchronizer show all
Defined in:
lib/jekyll/image_optimizer/image_optimizer.rb

Instance Method Summary collapse

Methods inherited from Synchronizer

#copy_file, #create_symlink, #delete_dir, #delete_file, #delete_targets_without_source, #synchronize, #synchronize_file

Constructor Details

#initialize(source_path, target_path, use_hash) ⇒ ImageOptimizer

Returns a new instance of ImageOptimizer.



5
6
7
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 5

def initialize(source_path, target_path, use_hash)
  super(source_path, target_path, use_hash)
end

Instance Method Details

#do_synchronize_file(source, target, param) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 17

def do_synchronize_file(source, target, param)
  if process? source
    puts "optimizing #{source} -> #{target}"
    optimize_image(source, target, param)
  else
    puts "copying #{source} -> #{target}"
    copy_file(source, target)
  end
end

#optimize_image(source, target, geometry) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 27

def optimize_image(source, target, geometry)
  image = Magick::Image.read(source).first
  image.change_geometry!(geometry) { |cols, rows, img|
    img.resize!(cols, rows)
    img.strip!
    img.write('jpeg:'+target)
  }
end

#optimize_images(geometry) ⇒ Object



9
10
11
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 9

def optimize_images(geometry)
  synchronize(geometry)
end

#process?(file) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 36

def process?(file)
  file.downcase.end_with? '.jpg' or file.downcase.end_with? '.jpeg'
end

#target_name(path, geometry) ⇒ Object



13
14
15
# File 'lib/jekyll/image_optimizer/image_optimizer.rb', line 13

def target_name(path, geometry)
  path+geometry.gsub('>', 'max').gsub('<', 'min').gsub('@', 'area')
end