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"- CWEBP_CMD =
"cwebp"- RESIZE_CMD =
"mogrify -background white -resize %dx\\> "- ROTATE_CMD =
"exiftran -ai "- COMPRESS_CMD =
"jpegoptim --strip-all --max=90 "- EXTERNAL_CMDS =
[CONVERT_CMD, RESIZE_CMD, ROTATE_CMD, COMPRESS_CMD, DWEBP_CMD, CWEBP_CMD]
- WEBP_SEARCH =
/(.+)\.webp/i- PNG_SEARCH =
/(.+)\.png/i- JPG_SEARCH =
/(.+)\.jpe?g/i- HEIC_SEARCH =
/(.+)\.heic/i- WEBP_REPLACE =
'\1.webp'- JPG_REPLACE =
'\1.jpg'- PNG_REPLACE =
'\1.png'- EXCLUDE_PAT =
先頭が“_”の場合は除外
/^_/
Class Method Summary collapse
- .cmd_exists?(cmd) ⇒ Boolean
- .filter_files(argv) ⇒ Object
- .match_exclude_image?(filename) ⇒ Boolean
- .replace_heic2jpg(filename) ⇒ Object
- .replace_image2webp(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.
206 207 208 209 |
# File 'lib/imagetools/imagefilter.rb', line 206 def initialize(opts, config) @opts = opts @config = config end |
Class Method Details
.cmd_exists?(cmd) ⇒ Boolean
201 202 203 204 |
# File 'lib/imagetools/imagefilter.rb', line 201 def self.cmd_exists?(cmd) # whichはコマンドが存在する場合のみ標準出力にパスを出力する `which #{cmd}` != "" end |
.filter_files(argv) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/imagetools/imagefilter.rb', line 138 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
184 185 186 |
# File 'lib/imagetools/imagefilter.rb', line 184 def self.match_exclude_image?(filename) filename =~ EXCLUDE_PAT end |
.replace_heic2jpg(filename) ⇒ Object
168 169 170 |
# File 'lib/imagetools/imagefilter.rb', line 168 def self.replace_heic2jpg(filename) filename.sub(HEIC_SEARCH, JPG_REPLACE) end |
.replace_image2webp(filename) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/imagetools/imagefilter.rb', line 172 def self.replace_image2webp(filename) # PNGとJPGに対応(WEBPはそのまま) str = filename.dup if str.sub!(PNG_SEARCH, WEBP_REPLACE) return str end if str.sub!(JPG_SEARCH, WEBP_REPLACE) return str end return str end |
.replace_image_filename(filename, patterns) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/imagetools/imagefilter.rb', line 149 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
164 165 166 |
# File 'lib/imagetools/imagefilter.rb', line 164 def self.replace_png2jpg(filename) filename.sub(PNG_SEARCH, JPG_REPLACE) end |
.replace_webp2png(filename) ⇒ Object
160 161 162 |
# File 'lib/imagetools/imagefilter.rb', line 160 def self.replace_webp2png(filename) filename.sub(WEBP_SEARCH, PNG_REPLACE) end |
.run(argv) ⇒ Object
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 131 132 133 134 135 136 |
# File 'lib/imagetools/imagefilter.rb', line 86 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 } types = ['jpg', 'wepp'] opt.on('-output-type=OUTPUTTYPE', types, types.join("|") + "(default jpg)") {|v| opts[:o] = v} opt.parse!(argv) opts[:o] ||= types[0] 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
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/imagetools/imagefilter.rb', line 189 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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/imagetools/imagefilter.rb', line 211 def run(filepath) if exclude_image?(filepath) return end # ファイル名変換 filepath = rename_image(filepath) # filepath = webp2png(filepath) # filepath = png2jpg(filepath) # HEICをJPEGに filepath = heic2jpg(filepath) # JPEGの回転 filepath = rotate_jpg(filepath) # JPEG/PNG/WEBP 画像のリサイズ filepath = resize_image(filepath) if @opts[:o] == 'webp' # JPEG/PNGをWEBPに変換(WEBPはそのまま) filepath = image2webp(filepath) else # PNGとWebPをJPEGに変換 filepath = webp2png(filepath) filepath = png2jpg(filepath) filepath = compress_jpg(filepath) end filepath end |