Class: Imagetools::Imageresizer

Inherits:
Object
  • Object
show all
Defined in:
lib/imagetools/imageresizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Imageresizer

Returns a new instance of Imageresizer.



45
46
47
# File 'lib/imagetools/imageresizer.rb', line 45

def initialize(opts)
  @opts = opts
end

Class Method Details

.get_image_files(argv) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/imagetools/imageresizer.rb', line 34

def self.get_image_files(argv)
  image_files = []
  argv.each do |arg|
    arg = File.expand_path(arg)
    if FileTest.file?(arg) && (arg =~ /\.jpe?g$/i || arg =~ /\.png/i)
      image_files << arg
    end        
  end
  image_files
end

.run(argv) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/imagetools/imageresizer.rb', line 9

def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.banner = "Usage: #{opt.program_name} [-h|--help] <args>"
  opt.version = VERSION
  opt.separator('')      
  opt.separator("Options:")
  opt.on_head('-h', '--help', 'Show this message') do |v|
    puts opt.help
    exit
  end
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
  opt.on('--dry-run', 'Message only') {|v| opts[:dry_run] = v}
  opt.on('-o OUTNAME', '--output=OUTNAME', 'Output file') {|v| opts[:o] = v}      
  opt.parse!(argv)
  image_files = get_image_files(argv)
  if image_files.size < 1
    puts opt.help
    exit                
  end
  command = Imageresizer.new(opts)
  command.run(image_files[0])  
end

Instance Method Details

#run(image_file) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/imagetools/imageresizer.rb', line 49

def run(image_file)
  imgdata = File.binread(image_file)
  resultdata = center_and_pad(imgdata, 960, 640, 'white', ::Magick::CenterGravity)
  
  outpath = @opts[:o] || 'image.png'
  puts "write to #{outpath}"
  File.binwrite("image.png", resultdata)
end