Module: Vivlio::Starter::CLI::ResizeCommands
- Defined in:
- lib/vivlio/starter/cli/resize.rb
Overview
Module: 画像リサイズ/変換ロジック
提供機能:
- 画像を WebP に変換(高精細/標準/軽量のプリセット)
Constant Summary collapse
- RESIZE_DESC =
{ high: { short: '画像を高品質WebPに変換します', long: <<~DESC 画像を高品質WebPに変換します(quality=90, max_px=2000)。 対象: .png, .jpg, .jpeg 出力: 同ディレクトリに .webp 引数: DIR 対象ディレクトリ(省略時は images/) 使用例: vs resize:high vs resize:high assets/images DESC }, medium: { short: '画像を標準品質WebPに変換します', long: <<~DESC 画像を標準品質WebPに変換します(quality=85, max_px=1600)。 対象: .png, .jpg, .jpeg 出力: 同ディレクトリに .webp 引数: DIR 対象ディレクトリ(省略時は images/) 使用例: vs resize:medium vs resize:medium assets/images DESC }, low: { short: '画像を軽量品質WebPに変換します', long: <<~DESC 画像を軽量品質WebPに変換します(quality=75, max_px=1200)。 対象: .png, .jpg, .jpeg 出力: 同ディレクトリに .webp 引数: DIR 対象ディレクトリ(省略時は images/) 使用例: vs resize:low vs resize:low assets/images DESC }, default: { short: '画像をWebPに変換します(標準品質)', long: <<~DESC 画像をWebPに変換します(標準品質が既定)。 対象: .png, .jpg, .jpeg 出力: 同ディレクトリに .webp 引数: DIR 対象ディレクトリ(省略時は images/) オプション: --force 既存ファイルも強制再生成 --high 高品質プリセットを使用 --low 軽量品質プリセットを使用 使用例: vs resize vs resize assets/images vs resize --high vs resize --force DESC } }.freeze
Class Method Summary collapse
-
.execute_resize_high(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 高品質プリセット.
-
.execute_resize_low(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 軽量品質プリセット.
-
.execute_resize_medium(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 標準品質プリセット.
-
.execute_resize_with_preset(preset_name, dir, options = {}) ⇒ Object
Samovar/直接呼び出し用: プリセット指定でリサイズ.
- .included(base) ⇒ Object
Class Method Details
.execute_resize_high(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 高品質プリセット
94 95 96 |
# File 'lib/vivlio/starter/cli/resize.rb', line 94 def execute_resize_high(dir = 'images', = {}) execute_resize_with_preset('高精細', dir, ) end |
.execute_resize_low(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 軽量品質プリセット
106 107 108 |
# File 'lib/vivlio/starter/cli/resize.rb', line 106 def execute_resize_low(dir = 'images', = {}) execute_resize_with_preset('軽量', dir, ) end |
.execute_resize_medium(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 標準品質プリセット
100 101 102 |
# File 'lib/vivlio/starter/cli/resize.rb', line 100 def execute_resize_medium(dir = 'images', = {}) execute_resize_with_preset('標準', dir, ) end |
.execute_resize_with_preset(preset_name, dir, options = {}) ⇒ Object
Samovar/直接呼び出し用: プリセット指定でリサイズ
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/vivlio/starter/cli/resize.rb', line 112 def execute_resize_with_preset(preset_name, dir, = {}) ENV['VERBOSE'] = '1' if [:verbose] ENV['FORCE'] = '1' if [:force] presets = { '高精細' => { quality: 90, method: 6, max_px: 2000 }, '標準' => { quality: 85, method: 6, max_px: 1600 }, '軽量' => { quality: 75, method: 6, max_px: 1200 } } preset = presets[preset_name] unless preset Common.log_error("未知のプリセットです: #{preset_name}") return end unless Dir.exist?(dir) Common.log_error("ディレクトリが存在しません: #{dir}") return end unless system('which magick >/dev/null 2>&1') Common.log_error('Error: ImageMagick (magick) が見つかりません。brew install imagemagick 等で導入してください。') return end patterns = %w[png jpg jpeg JPG JPEG PNG] files = patterns.flat_map { |ext| Dir.glob(File.join(dir, "**/*.#{ext}")) }.uniq.sort if files.empty? Common.log_info("対象画像が見つかりませんでした: #{dir}") return end Common.log_action("画像変換を開始: Preset=#{preset_name}, Dir=#{dir}, Files=#{files.size}") files.each do |src| dst = src.sub(/\.[^.]+\z/, '.webp') if ENV['FORCE'].nil? && File.exist?(dst) && File.mtime(dst) >= File.mtime(src) Common.log_info("skip: up-to-date #{dst}") next end FileUtils.mkdir_p(File.dirname(dst)) cmd = [ 'magick', src, '-resize', "#{preset[:max_px]}x#{preset[:max_px]}>", '-strip', '-quality', preset[:quality].to_s, '-define', "webp:method=#{preset[:method]}", dst ] Common.log_info(cmd.join(' ')) unless system(*cmd) Common.log_error("変換に失敗しました: #{src}") return end end Common.log_success('画像変換が完了しました') # --delete-originals: 変換成功した元ファイルを確認後に削除 return unless [:delete_originals] converted_originals = files.select { |src| File.exist?(src.sub(/\.[^.]+\z/, '.webp')) } if converted_originals.empty? Common.log_info('削除対象の元ファイルはありませんでした') else Common.log_always('⚠️ 以下の元画像ファイルを削除しようとしています:') converted_originals.each { |f| Common.log_always(" - #{f}") } $stdout.print('本当に削除しますか? [y/N]: ') ans = $stdin.gets if ans && ans.strip.downcase == 'y' converted_originals.each do |f| FileUtils.rm_f(f) Common.log_info("削除しました: #{f}") end Common.log_success("元ファイルを削除しました(#{converted_originals.size}件)") else Common.log_info('元ファイルの削除をキャンセルしました') end end end |
.included(base) ⇒ Object
91 |
# File 'lib/vivlio/starter/cli/resize.rb', line 91 def included(base); end |