Class: Imagetools::Imageblog

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Imageblog

Returns a new instance of Imageblog.



89
90
91
# File 'lib/imagetools/imageblog.rb', line 89

def initialize(opts)
  @opts = opts
end

Class Method Details

.get_image_files(opts, argv) ⇒ Object



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
80
81
82
83
84
85
86
87
# File 'lib/imagetools/imageblog.rb', line 51

def self.get_image_files(opts, argv)
  image_files = []
  # ディレクトリが処理対象かどうかを決める
  dir = nil
  if argv.size == 1
    # 引き数が1個の場合は最初の引き数
    path = File.expand_path(argv[0])
    if FileTest.directory?(path)
      dir = path
    end
  elsif argv.size == 0
    # 引き数がない場合カレントディレクトリ
    dir = File.expand_path('.')
  end
  if dir
    concat_number = opts[:n] || 2
    # ディレクトリが指定されていた場合、指定ディレクトリ内のIMG_ファイルの最新n個を対象とする
    # 最新の基準は(ファイル名基準)
    match_files = Dir.glob("#{dir}/*.{jpg,jpeg,png}", File::FNM_CASEFOLD).sort
#        match_files.sort {|a, b|
#          File.mtime(a) <=> File.mtime(b)
#        }
    # 後ろからn個を取得(小さい方の数とする)
    count = [match_files.size, concat_number].min
    image_files = match_files[-count..-1]
  else
    # それ以外は指定された引き数を全て対象とする
    argv.each do |arg|
      arg = File.expand_path(arg)
      dir = File.dirname(arg)
      if FileTest.file?(arg) && (arg =~ /\.jpe?g$/i || arg =~ /\.png/i)
        image_files << arg
      end
    end
  end
  return dir, image_files
end

.run(argv) ⇒ Object



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
# File 'lib/imagetools/imageblog.rb', line 19

def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.version = VERSION
  opt.banner = "Usage: #{opt.program_name} [-h|--help] <dir> or <image1 image2 image3 ...>"
  opt.separator('')
  opt.separator("Examples:")
  opt.separator("    #{opt.program_name} ~/tmp # concat two recent IMG_*jpg images.")
  opt.separator("    #{opt.program_name} image1.jpg image2.jpg image3.jpg  # concat specified images.")
  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 OUTDIR', '--output=OUTDIR', 'Output dir') {|v| opts[:o] = v}
  opt.on('-n NUM', '--number=NUM', 'Process image number') {|v| opts[:n] = v.to_i}
  opt.on('-b BASENAME', '--base=BASENAME', 'Output file basename') {|v| opts[:b] = v} 
  opt.parse!(argv)
  opts[:b] ||= Time.now.strftime("%Y%m%d")
  dir, image_files = get_image_files(opts, argv)
  if image_files.size == 0
    puts opt.help
    exit        
  end
  command = Imageblog.new(opts)
  command.run(dir, image_files)
end

Instance Method Details

#run(dir, image_files) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/imagetools/imageblog.rb', line 94

def run(dir, image_files)
  outdir = dir
  if @opts[:o]
    outdir = @opts[:o]
  end
  rename_images(image_files, outdir)
#      concat_images(image_files, output_path)        
end