Class: Imagetools::Imagefilter
- Inherits:
-
Object
- Object
- Imagetools::Imagefilter
- Defined in:
- lib/imagetools/imagefilter.rb
Constant Summary collapse
- OTHER_JPG_SEARCH =
/\.(large|huge|jpg_large|JPG)$/i- OTHER_JPG_REPLACE =
'.jpg'- CONVERT_CMD =
"convert"- DWEBP_CMD =
"dwebp"- RESIZE_CMD =
"mogrify -background white -resize %dx\\> "- 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.png'- PNG_SEARCH =
/(.+)\.png/i- PNG_REPLACE =
'\1.jpg'- JPG_SEARCH =
/(.+)\.jpe?g/i- HEIC_SEARCH =
/(.+)\.heic/i- HEIC_REPLACE =
'\1.jpg'- EXCLUDE_PAT =
先頭が“_”の場合は除外
/^_/
Class Method Summary collapse
- .cmd_exists?(cmd) ⇒ Boolean
- .filter_files(argv) ⇒ Object
- .match_exclude_image?(filename) ⇒ Boolean
- .replace_heic2jpg(filename) ⇒ Object
- .replace_image_filename(filename, patterns) ⇒ Object
- .replace_png2jpg(filename) ⇒ Object
- .replace_webp2png(filename) ⇒ Object
- .run(argv) ⇒ Object
- .selftest ⇒ Object
Instance Method Summary collapse
-
#initialize(opts, config) ⇒ Imagefilter
constructor
A new instance of Imagefilter.
- #run(filepath) ⇒ Object
Constructor Details
#initialize(opts, config) ⇒ Imagefilter
Returns a new instance of Imagefilter.
187 188 189 190 |
# File 'lib/imagetools/imagefilter.rb', line 187 def initialize(opts, config) @opts = opts @config = config end |
Class Method Details
.cmd_exists?(cmd) ⇒ Boolean
182 183 184 185 |
# File 'lib/imagetools/imagefilter.rb', line 182 def self.cmd_exists?(cmd) # whichはコマンドが存在する場合のみ標準出力にパスを出力する `which #{cmd}` != "" end |
.filter_files(argv) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/imagetools/imagefilter.rb', line 132 def self.filter_files(argv) filepaths = [] argv.each do |arg| if FileTest.file?(arg) path = File.(arg) filepaths << path end end filepaths end |
.match_exclude_image?(filename) ⇒ Boolean
166 167 168 |
# File 'lib/imagetools/imagefilter.rb', line 166 def self.match_exclude_image?(filename) filename =~ EXCLUDE_PAT end |
.replace_heic2jpg(filename) ⇒ Object
162 163 164 |
# File 'lib/imagetools/imagefilter.rb', line 162 def self.replace_heic2jpg(filename) filename.sub(HEIC_SEARCH, HEIC_REPLACE) end |
.replace_image_filename(filename, patterns) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/imagetools/imagefilter.rb', line 143 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) ⇒ Object
158 159 160 |
# File 'lib/imagetools/imagefilter.rb', line 158 def self.replace_png2jpg(filename) filename.sub(PNG_SEARCH, PNG_REPLACE) end |
.replace_webp2png(filename) ⇒ Object
154 155 156 |
# File 'lib/imagetools/imagefilter.rb', line 154 def self.replace_webp2png(filename) filename.sub(WEBP_SEARCH, WEBP_REPLACE) end |
.run(argv) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/imagetools/imagefilter.rb', line 83 def self.run(argv) STDOUT.sync = true opts = {} opt = OptionParser.new(argv) opt. = "Usage: #{opt.program_name} [-h|--help] <args>" opt.version = VERSION opt.separator('') opt.separator('Parameters:') param ="RESIZE_CMD: \#{RESIZE_CMD}\nROTATE_CMD: \#{ROTATE_CMD} \nCOMPRESS_CMD: \#{COMPRESS_CMD}\n" 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.(config_file) yaml = nil if FileTest.file?(config_file) yaml = YAML.load_file(config_file) end config = Config.create_from_yaml(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 ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/imagetools/imagefilter.rb', line 170 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 |
Instance Method Details
#run(filepath) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/imagetools/imagefilter.rb', line 192 def run(filepath) if exclude_image?(filepath) return end filepath = rename_image(filepath) filepath = webp2png(filepath) filepath = png2jpg(filepath) filepath = heic2jpg(filepath) filepath = resize_jpg(filepath) filepath = rotate_jpg(filepath) filepath = compress_jpg(filepath) filepath end |