|
| 1 | +<# |
| 2 | +Script: generate-thumbnails.ps1 |
| 3 | +Purpose: Generate thumbnails for images in this folder and write them to ./thumbnails with a -thumb suffix. |
| 4 | +Requirements: |
| 5 | + - ImageMagick (magick) recommended |
| 6 | + - On Windows, PowerShell can use System.Drawing as a fallback |
| 7 | +
|
| 8 | +Usage: |
| 9 | + powershell -NoProfile -ExecutionPolicy Bypass -File .\generate-thumbnails.ps1 -Width 300 |
| 10 | +#> |
| 11 | + |
| 12 | +param( |
| 13 | + [int]$Width = 300, |
| 14 | + [string]$SourceDir = ".", |
| 15 | + [string]$OutDir = "./thumbnails", |
| 16 | + [switch]$Force |
| 17 | +) |
| 18 | + |
| 19 | +$ErrorActionPreference = 'Stop' |
| 20 | + |
| 21 | +if (-not (Test-Path $OutDir)) { |
| 22 | + New-Item -ItemType Directory -Path $OutDir | Out-Null |
| 23 | +} |
| 24 | + |
| 25 | +# Collect images using explicit patterns to avoid Get-ChildItem -Include pitfalls |
| 26 | +$images = @() |
| 27 | +$patterns = @('*.png','*.jpg','*.jpeg','*.gif') |
| 28 | +foreach ($p in $patterns) { |
| 29 | + $images += Get-ChildItem -Path (Join-Path $SourceDir $p) -File -ErrorAction SilentlyContinue |
| 30 | +} |
| 31 | +# Remove duplicates and normalize |
| 32 | +$images = $images | Sort-Object -Property FullName -Unique |
| 33 | + |
| 34 | +Write-Output "Found $($images.Count) image(s) in $SourceDir" |
| 35 | + |
| 36 | +foreach ($img in $images) { |
| 37 | + $thumbName = [System.IO.Path]::Combine($OutDir, "$($img.BaseName)-thumb$($img.Extension)") |
| 38 | + if ((Test-Path $thumbName) -and -not $Force) { |
| 39 | + Write-Output "Skipping existing: $thumbName" |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + # Prefer ImageMagick if available |
| 44 | + $magick = Get-Command magick -ErrorAction SilentlyContinue |
| 45 | + if ($magick) { |
| 46 | + Write-Output "Creating thumbnail via ImageMagick: $thumbName" |
| 47 | + # Use 'magick' in a portable way: magick input -auto-orient -thumbnail {Width}x output |
| 48 | + & magick "$($img.FullName)" -auto-orient -thumbnail ${Width}x "$thumbName" |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + # Fallback: use .NET System.Drawing (Windows only) |
| 53 | + try { |
| 54 | + Add-Type -AssemblyName System.Drawing |
| 55 | + $src = [System.Drawing.Image]::FromFile($img.FullName) |
| 56 | + $ratio = [double]$Width / $src.Width |
| 57 | + $newHeight = [int]([math]::Round($src.Height * $ratio)) |
| 58 | + $thumb = New-Object System.Drawing.Bitmap $Width, $newHeight |
| 59 | + $g = [System.Drawing.Graphics]::FromImage($thumb) |
| 60 | + $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic |
| 61 | + $g.DrawImage($src, 0, 0, $Width, $newHeight) |
| 62 | + $thumb.Save($thumbName, $src.RawFormat) |
| 63 | + $g.Dispose() |
| 64 | + $thumb.Dispose() |
| 65 | + $src.Dispose() |
| 66 | + Write-Output "Created thumbnail: $thumbName" |
| 67 | + } |
| 68 | + catch { |
| 69 | + Write-Warning "Failed to create thumbnail for $($img.Name): $_" |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +Write-Output "Done. Thumbnails are in: $OutDir" |
0 commit comments