Class: Imagetools::Imagefilter
- Inherits:
-
Object
- Object
- Imagetools::Imagefilter
- Defined in:
- lib/imagetools/imagefilter.rb
Constant Summary collapse
- 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 =
先頭が“_”の場合は除外
/^_/
Class Method Summary collapse
- .cmd_exists?(cmd) ⇒ Boolean
- .filter_files(argv) ⇒ Object
- .match_exclude_image?(filename) ⇒ Boolean
- .replace_image_filename(filename, patterns) ⇒ Object
- .replace_png2jpg(filename) ⇒ Object
- .replace_webp2jpg(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.
165 166 167 168 |
# File 'lib/imagetools/imagefilter.rb', line 165 def initialize(opts, config) @opts = opts @config = config end |
Class Method Details
.cmd_exists?(cmd) ⇒ Boolean
160 161 162 163 |
# File 'lib/imagetools/imagefilter.rb', line 160 def self.cmd_exists?(cmd) # whichはコマンドが存在する場合のみ標準出力にパスを出力する `which #{cmd}` != "" end |
.filter_files(argv) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/imagetools/imagefilter.rb', line 114 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
144 145 146 |
# File 'lib/imagetools/imagefilter.rb', line 144 def self.match_exclude_image?(filename) filename =~ EXCLUDE_PAT end |
.replace_image_filename(filename, patterns) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/imagetools/imagefilter.rb', line 125 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
140 141 142 |
# File 'lib/imagetools/imagefilter.rb', line 140 def self.replace_png2jpg(filename) filename.sub(PNG_SEARCH, PNG_REPLACE) end |
.replace_webp2jpg(filename) ⇒ Object
136 137 138 |
# File 'lib/imagetools/imagefilter.rb', line 136 def self.replace_webp2jpg(filename) filename.sub(WEBP_SEARCH, WEBP_REPLACE) end |
.run(argv) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 |
# File 'lib/imagetools/imagefilter.rb', line 65 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 =<<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.(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 ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/imagetools/imagefilter.rb', line 148 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
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/imagetools/imagefilter.rb', line 170 def run(filepath) if exclude_image?(filepath) return end filepath = rename_image(filepath) filepath = webp2jpg(filepath) filepath = png2jpg(filepath) filepath = resize_jpg(filepath) filepath = rotate_jpg(filepath) filepath = compress_jpg(filepath) filepath end |