Class: Plans::Thumbs

Inherits:
Command show all
Defined in:
lib/plans/thumbs.rb

Instance Attribute Summary

Attributes inherited from Command

#options, #shell

Instance Method Summary collapse

Methods inherited from Command

#check_plans_pathname_exists, #initialize, #pathname, #plans_pathname, #raise_error, source_root

Constructor Details

This class inherits a constructor from Plans::Command

Instance Method Details

#create_thumbnails(size, images, path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/plans/thumbs.rb', line 30

def create_thumbnails(size, images, path)
  target_directory = path + "#{size}px"
  FileUtils.remove_dir target_directory if Dir.exist? target_directory
  FileUtils.mkdir target_directory
  FileUtils.cp images, target_directory
  `mogrify -resize #{size} #{target_directory}/*.*`
  # Check mogrify's return code.
  if $?.to_i == 0
    say "Created #{images.length} #{size}px images."
  else
    say "Problem creating #{size}px images. (Mogrify ERR: #{$?.to_i})", :red
    say "  #{target_directory}"
    raise_error("Mogrify ERR: #{$?.to_i}")
  end
end

#do(path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/plans/thumbs.rb', line 6

def do(path)
  img_path = pathname(path) + 'img'

  unless img_path.exist?
    say 'Images directory (img) does not exist.'
    say "  #{img_path}"
    if (yes? 'Would you like to create the directory?')
      say 'Creating it.'
      FileUtils.mkdir img_path
    end
  end

  images = Dir.glob(img_path + '*.*')
  if (images.length == 0)
    say 'Nothing to do. No images found in img directory.', :green
    say "  #{img_path}"
    return
  end
  create_thumbnails(200, images, img_path)
  create_thumbnails(400, images, img_path)
  create_thumbnails(600, images, img_path)
  say 'Thumbnails creation complete.', :green
end