# coding: utf-8 require 'imagetools/version' require 'fileutils' require 'optparse' require 'yaml' module Imagetools class Config FILENAME_SEARCH = 's (\d+)-(\d+)-(\d+) (\d+)\.(\d+)\.(\d+)' FILENAME_REPLACE = 's_\1\2\3_\4\5\6' def initialize(yaml) @yaml = yaml @filename_search1 = config_value("filename", "search1", false) || FILENAME_SEARCH @filename_replace1 = config_value("filename", "replace1", false) || FILENAME_REPLACE @filename_search2 = config_value("filename", "search2", false) @filename_replace2 = config_value("filename", "replace2", false) @filename_search3 = config_value("filename", "search3", false) @filename_replace3 = config_value("filename", "replace3", false) end attr_reader :filename_search1, :filename_replace1, :filename_search2, :filename_replace2, :filename_search3, :filename_replace3 def filename_patterns [ [@filename_search1, @filename_replace1], [@filename_search2, @filename_replace2], [@filename_search3, @filename_replace3], ] end private def config_value(section, key, require) return nil unless @yaml value = @yaml[section][key] if require && (value.nil? || value.empty?) raise RuntimeError, "#{section}:#{key}: is empty" end value end end class Imagefilter OTHER_JPG_SEARCH = /\.(large|huge|jpg_large)$/i OTHER_JPG_REPLACE = '.jpg' CONVERT_CMD = "convert" DWEBP_CMD = "dwebp" RESIZE_CMD = "mogrify -resize 1280x\\> " ROTATE_CMD = "exiftran -ai " COMPRESS_CMD = "jpegoptim --strip-all --max=90 " EXTERNAL_CMDS = [RESIZE_CMD, ROTATE_CMD, COMPRESS_CMD] WEBP_SEARCH = /(.+)\.webp/i WEBP_REPLACE = '\1.jpg' PNG_SEARCH = /(.+)\.png/i PNG_REPLACE = '\1.jpg' JPG_SEARCH = /(.+)\.jpe?g/i EXCLUDE_PAT = /^_/ # 先頭が"_"の場合は除外 def self.run(argv) STDOUT.sync = true opts = {} opt = OptionParser.new(argv) opt.banner = "Usage: #{opt.program_name} [-h|--help] " opt.version = VERSION opt.separator('') opt.separator('Parameters:') param =< #{topath}" FileUtils.mv(filepath, topath) unless @opts[:n] return topath end def webp2jpg(filepath) fromname = File.basename(filepath) toname = self.class.replace_webp2jpg(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 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 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 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 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 end end