class Imagetools::Imagefilter
Constants
- COMPRESS_CMD
- CONVERT_CMD
- DWEBP_CMD
- EXCLUDE_PAT
- EXTERNAL_CMDS
- JPG_SEARCH
- OTHER_JPG_REPLACE
- OTHER_JPG_SEARCH
- PNG_REPLACE
- PNG_SEARCH
- RESIZE_CMD
RESIZE_CMD
= “mogrify -resize 1280x\> ”- ROTATE_CMD
- WEBP_REPLACE
- WEBP_SEARCH
Public Class Methods
cmd_exists?(cmd)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 161 def self.cmd_exists?(cmd) # whichはコマンドが存在する場合のみ標準出力にパスを出力する `which #{cmd}` != "" end
filter_files(argv)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 115 def self.filter_files(argv) filepaths = [] argv.each do |arg| if FileTest.file?(arg) path = File.expand_path(arg) filepaths << path end end filepaths end
match_exclude_image?(filename)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 145 def self.match_exclude_image?(filename) filename =~ EXCLUDE_PAT end
new(opts, config)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 166 def initialize(opts, config) @opts = opts @config = config end
replace_image_filename(filename, patterns)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 126 def self.replace_image_filename(filename, patterns) filename = filename.dup patterns.each do |search, replace| if search && replace reg = Regexp.new(search, Regexp::IGNORECASE) filename = filename.sub(reg, replace) end end filename.sub(OTHER_JPG_SEARCH, OTHER_JPG_REPLACE) end
replace_png2jpg(filename)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 141 def self.replace_png2jpg(filename) filename.sub(PNG_SEARCH, PNG_REPLACE) end
replace_webp2png(filename)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 137 def self.replace_webp2png(filename) filename.sub(WEBP_SEARCH, WEBP_REPLACE) end
run(argv)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 66 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('Parameters:') param =<<EOM RESIZE_CMD: #{RESIZE_CMD} ROTATE_CMD: #{ROTATE_CMD} COMPRESS_CMD: #{COMPRESS_CMD} EOM opt.separator(param) 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('-n', '--dry-run', 'Message only') {|v| opts[:n] = v} opt.on('-t', '--self-test', 'Run Self Test') {|v| opts[:t] = v} opt.on('-c', '--config', 'Config file'){|v| opts[:c] = v } opt.parse!(argv) config_file = opts[:c] || "~/.imagefilterrc" config_file = File.expand_path(config_file) yaml = nil if FileTest.file?(config_file) yaml = YAML.load_file(config_file) end config = Config.new(yaml) if opts[:t] ret = selftest exit(ret) end filepaths = self.filter_files(argv) if filepaths.empty? puts opt.help exit end filepaths.each do |filepath| command = Imagefilter.new(opts, config) command.run(filepath) end end
selftest()
click to toggle source
# File lib/imagetools/imagefilter.rb, line 149 def self.selftest EXTERNAL_CMDS.each do |cmd| args = cmd.split cmd_name = args[0] unless cmd_exists?(cmd_name) puts "No command: #{cmd_name}" return 127 end end return 0 end
Public Instance Methods
run(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 171 def run(filepath) if exclude_image?(filepath) return end filepath = rename_image(filepath) filepath = webp2png(filepath) filepath = png2jpg(filepath) filepath = resize_jpg(filepath) filepath = rotate_jpg(filepath) filepath = compress_jpg(filepath) filepath end
Private Instance Methods
compress_jpg(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 261 def compress_jpg(filepath) fromname = File.basename(filepath) unless fromname =~ JPG_SEARCH return filepath end puts "compress: #{filepath}" cmd = "#{COMPRESS_CMD} \"#{filepath}\"" system(cmd) return filepath end
exclude_image?(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 186 def exclude_image?(filepath) fromname = File.basename(filepath) if self.class.match_exclude_image?(fromname) puts "exclude #{filepath}" return true end false end
png2jpg(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 223 def png2jpg(filepath) fromname = File.basename(filepath) toname = self.class.replace_png2jpg(fromname) return filepath if fromname == toname dir = File.dirname(filepath) topath = File.join(dir, toname) puts "convert: #{filepath} => #{topath}" # convert test.png -background "#ffff00" -flatten test.jpg cmd = "#{CONVERT_CMD} \"#{filepath}\" -background \"#ffffff\" -flatten \"#{topath}\"" if system(cmd) FileUtils.rm(filepath) end return topath end
rename_image(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 195 def rename_image(filepath) fromname = File.basename(filepath) toname = self.class.replace_image_filename(fromname, @config.filename_patterns) return filepath if fromname == toname dir = File.dirname(filepath) topath = File.join(dir, toname) puts "rename: #{filepath} => #{topath}" FileUtils.mv(filepath, topath) unless @opts[:n] return topath end
resize_jpg(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 239 def resize_jpg(filepath) fromname = File.basename(filepath) unless fromname =~ JPG_SEARCH return filepath end puts "resize: #{filepath}" cmd = "#{RESIZE_CMD} \"#{filepath}\"" system(cmd) return filepath end
rotate_jpg(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 250 def rotate_jpg(filepath) fromname = File.basename(filepath) unless fromname =~ JPG_SEARCH return filepath end puts "rotate: #{filepath}" cmd = "#{ROTATE_CMD} \"#{filepath}\"" system(cmd) return filepath end
webp2png(filepath)
click to toggle source
# File lib/imagetools/imagefilter.rb, line 207 def webp2png(filepath) fromname = File.basename(filepath) toname = self.class.replace_webp2png(fromname) return filepath if fromname == toname dir = File.dirname(filepath) topath = File.join(dir, toname) puts "convert: #{filepath} => #{topath}" # dwebp ~/Desktop/1.webp -o ~/Desktop/1.jpg cmd = "#{DWEBP_CMD} \"#{filepath}\" -o \"#{topath}\"" if system(cmd) FileUtils.rm(filepath) end return topath end