Module: VivlioStarter::CLI::ResizeCommands
- Defined in:
- lib/vivlio_starter/cli/resize.rb
Overview
================================================================
Module: 画像リサイズ/変換ロジック
提供機能:
- 画像を WebP に変換(高精細/標準/軽量のプリセット)
- SVG を rsvg-convert → lossless WebP に変換(Techbook モード用)
================================================================
Defined Under Namespace
Classes: ResizeSummary
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
-
.convert_svg_to_webp(dirs, dpi: 350) ⇒ Object
SVG → PNG(rsvg-convert)→ lossless WebP(magick)変換 Chromium PDF エンジンが SVG 内の
/ を Type 3 フォントとして 埋め込む問題を回避するため、ビルド前に全 SVG をラスタライズする。. -
.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
.convert_svg_to_webp(dirs, dpi: 350) ⇒ Object
SVG → PNG(rsvg-convert)→ lossless WebP(magick)変換
Chromium PDF エンジンが SVG 内の
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/vivlio_starter/cli/resize.rb', line 220 def convert_svg_to_webp(dirs, dpi: 350) # --- Phase: ツール存在チェック --- unless system('which rsvg-convert >/dev/null 2>&1') Common.log_error('Error: rsvg-convert が見つかりません。brew install librsvg 等で導入してください。') return end unless system('which magick >/dev/null 2>&1') Common.log_error('Error: ImageMagick (magick) が見つかりません。brew install imagemagick 等で導入してください。') return end # --- Phase: SVG ファイル収集 --- svg_files = dirs .select { Dir.exist?(it) } .flat_map { Dir.glob(File.join(it, '**/*.svg')) } .uniq.sort if svg_files.empty? Common.log_info('[SVG→WebP] 対象 SVG ファイルが見つかりませんでした') return end Common.log_action("[SVG→WebP] #{svg_files.size} 件の SVG を変換します(DPI=#{dpi})") # --- Phase: 変換実行 --- converted = 0 svg_files.each do |svg_path| webp_path = svg_path.sub(/\.svg\z/i, '.webp') # mtime 比較でスキップ(--force 時は強制再生成) if ENV['FORCE'].nil? && File.exist?(webp_path) && File.mtime(webp_path) >= File.mtime(svg_path) Common.log_info("[SVG→WebP] skip: up-to-date #{webp_path}") next end # Step 1: SVG → PNG(rsvg-convert で高品質ラスタライズ) png_tmp = svg_path.sub(/\.svg\z/i, '.svg.tmp.png') rsvg_cmd = ['rsvg-convert', '--dpi-x', dpi.to_s, '--dpi-y', dpi.to_s, '-f', 'png', svg_path, '-o', png_tmp] Common.log_info("[SVG→WebP] rsvg-convert: #{svg_path}") unless system(*rsvg_cmd) Common.log_warn("[SVG→WebP] rsvg-convert に失敗しました: #{svg_path}") FileUtils.rm_f(png_tmp) next end # Step 2: PNG → lossless WebP(magick で可逆圧縮) magick_cmd = ['magick', png_tmp, '-define', 'webp:lossless=true', '-define', 'webp:method=6', '-strip', webp_path] Common.log_info("[SVG→WebP] magick lossless: #{webp_path}") unless system(*magick_cmd) Common.log_warn("[SVG→WebP] magick 変換に失敗しました: #{png_tmp}") FileUtils.rm_f(png_tmp) next end # 中間 PNG を削除 FileUtils.rm_f(png_tmp) converted += 1 end Common.log_success("[SVG→WebP] #{converted} 件の SVG を lossless WebP に変換しました") end |
.execute_resize_high(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 高品質プリセット
102 103 104 |
# File 'lib/vivlio_starter/cli/resize.rb', line 102 def execute_resize_high(dir = 'images', = {}) execute_resize_with_preset('高精細', dir, ) end |
.execute_resize_low(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 軽量品質プリセット
114 115 116 |
# File 'lib/vivlio_starter/cli/resize.rb', line 114 def execute_resize_low(dir = 'images', = {}) execute_resize_with_preset('軽量', dir, ) end |
.execute_resize_medium(dir = 'images', options = {}) ⇒ Object
Samovar/直接呼び出し用: 標準品質プリセット
108 109 110 |
# File 'lib/vivlio_starter/cli/resize.rb', line 108 def execute_resize_medium(dir = 'images', = {}) execute_resize_with_preset('標準', dir, ) end |
.execute_resize_with_preset(preset_name, dir, options = {}) ⇒ Object
Samovar/直接呼び出し用: プリセット指定でリサイズ
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/vivlio_starter/cli/resize.rb', line 120 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 ResizeSummary.new(converted: 0, skipped: 0) end Common.log_action("画像変換を開始: Preset=#{preset_name}, Dir=#{dir}, Files=#{files.size}") converted = 0 skipped = 0 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}") skipped += 1 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 ResizeSummary.new(converted: converted, skipped: skipped) end converted += 1 end Common.log_success('画像変換が完了しました') summary = ResizeSummary.new(converted: converted, skipped: skipped) # --delete-originals: 変換成功した元ファイルを確認後に削除 return summary unless [:delete_originals] converted_originals = files.select { |src| File.exist?(src.sub(/\.[^.]+\z/, '.webp')) } if converted_originals.empty? Common.log_info('削除対象の元ファイルはありませんでした') else Common.log_warn('以下の元画像ファイルを削除しようとしています:') converted_originals.each { |f| Common.log_always(" - #{f}") } if Common.confirm?('本当に削除しますか?') 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 summary end |
.included(base) ⇒ Object
91 |
# File 'lib/vivlio_starter/cli/resize.rb', line 91 def included(base); end |