FruitBank/Presentation/Nop.Web/Themes/CarHaven/Content/optimize-frames.ps1

166 lines
6.0 KiB
PowerShell

# =============================================
# OPTIMIZE AND REPACK ALL ANIMATION FRAMES
#
# Resizes vault frames from 1920px → 960px
# Resizes forklift frames from 1440px → 720px
# Recompresses both at JPEG quality 60
# Repacks into single bundle files
#
# Expected result:
# vault-bundle.jpg: ~10-13 MB (was ~52 MB)
# forklift-bundle.jpg: ~4-5 MB (was ~19 MB)
# =============================================
Add-Type -AssemblyName System.Drawing
function Resize-And-Compress {
param(
[string]$InputPath,
[string]$OutputPath,
[int]$MaxWidth,
[int]$Quality = 60
)
$img = [System.Drawing.Image]::FromFile($InputPath)
# Calculate new dimensions
if ($img.Width -gt $MaxWidth) {
$ratio = $MaxWidth / $img.Width
$newW = $MaxWidth
$newH = [int]($img.Height * $ratio)
} else {
$newW = $img.Width
$newH = $img.Height
}
$bmp = New-Object System.Drawing.Bitmap($newW, $newH)
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$g.DrawImage($img, 0, 0, $newW, $newH)
$g.Dispose()
$img.Dispose()
# Save with JPEG compression
$jpegCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' }
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, [long]$Quality)
$bmp.Save($OutputPath, $jpegCodec, $encoderParams)
$bmp.Dispose()
}
function Pack-Frames {
param(
[string]$FramesDir,
[string]$OutputFile,
[int]$Count
)
$frameBytes = @()
for ($i = 1; $i -le $Count; $i++) {
$path = Join-Path $FramesDir ("frame_{0:D4}.jpg" -f $i)
$frameBytes += ,([System.IO.File]::ReadAllBytes($path))
}
$headerSize = 4 + $Count * 8
$ms = New-Object System.IO.MemoryStream
$bw = New-Object System.IO.BinaryWriter($ms)
$bw.Write([uint32]$Count)
$currentOffset = $headerSize
for ($i = 0; $i -lt $Count; $i++) {
$bw.Write([uint32]$currentOffset)
$bw.Write([uint32]$frameBytes[$i].Length)
$currentOffset += $frameBytes[$i].Length
}
for ($i = 0; $i -lt $Count; $i++) {
$bw.Write($frameBytes[$i])
}
[System.IO.File]::WriteAllBytes($OutputFile, $ms.ToArray())
$bw.Close()
$ms.Close()
$size = (Get-Item $OutputFile).Length / 1MB
return [math]::Round($size, 1)
}
$basePath = "D:\REPOS\MANGO\source\FruitBank\Presentation\Nop.Web\Themes\CarHaven\Content"
# =============================================
# VAULT FRAMES: 323 frames, 1920px → 960px
# =============================================
$vaultDir = "$basePath\frames"
$vaultOptDir = "$basePath\frames-opt"
$vaultCount = 323
Write-Host "=== VAULT FRAMES ===" -ForegroundColor Cyan
Write-Host "Creating optimized directory..."
if (Test-Path $vaultOptDir) { Remove-Item $vaultOptDir -Recurse -Force }
New-Item -ItemType Directory -Path $vaultOptDir | Out-Null
Write-Host "Resizing $vaultCount frames to 960px, quality 60..."
for ($i = 1; $i -le $vaultCount; $i++) {
$src = Join-Path $vaultDir ("frame_{0:D4}.jpg" -f $i)
$dst = Join-Path $vaultOptDir ("frame_{0:D4}.jpg" -f $i)
Resize-And-Compress -InputPath $src -OutputPath $dst -MaxWidth 960 -Quality 60
if ($i % 50 -eq 0 -or $i -eq $vaultCount) {
$pct = [math]::Round(($i / $vaultCount) * 100)
Write-Host " [$pct%] Processed $i / $vaultCount"
}
}
# Check a sample
$sampleSize = (Get-Item "$vaultOptDir\frame_0050.jpg").Length / 1KB
Write-Host "Sample frame size: $([math]::Round($sampleSize, 0)) KB (was ~160 KB)" -ForegroundColor Yellow
Write-Host "Packing into vault-bundle.jpg..."
$vaultMB = Pack-Frames -FramesDir $vaultOptDir -OutputFile "$vaultDir\vault-bundle.jpg" -Count $vaultCount
Write-Host "vault-bundle.jpg: $vaultMB MB" -ForegroundColor Green
# =============================================
# FORKLIFT FRAMES: 120 frames, 1440px → 720px
# =============================================
$flDir = "$basePath\forklift-frames"
$flOptDir = "$basePath\forklift-frames-opt"
$flCount = 120
Write-Host ""
Write-Host "=== FORKLIFT FRAMES ===" -ForegroundColor Cyan
Write-Host "Creating optimized directory..."
if (Test-Path $flOptDir) { Remove-Item $flOptDir -Recurse -Force }
New-Item -ItemType Directory -Path $flOptDir | Out-Null
Write-Host "Resizing $flCount frames to 720px, quality 60..."
for ($i = 1; $i -le $flCount; $i++) {
$src = Join-Path $flDir ("frame_{0:D4}.jpg" -f $i)
$dst = Join-Path $flOptDir ("frame_{0:D4}.jpg" -f $i)
Resize-And-Compress -InputPath $src -OutputPath $dst -MaxWidth 720 -Quality 60
if ($i % 30 -eq 0 -or $i -eq $flCount) {
$pct = [math]::Round(($i / $flCount) * 100)
Write-Host " [$pct%] Processed $i / $flCount"
}
}
$sampleSize = (Get-Item "$flOptDir\frame_0050.jpg").Length / 1KB
Write-Host "Sample frame size: $([math]::Round($sampleSize, 0)) KB (was ~160 KB)" -ForegroundColor Yellow
Write-Host "Packing into forklift-bundle.jpg..."
$flMB = Pack-Frames -FramesDir $flOptDir -OutputFile "$flDir\forklift-bundle.jpg" -Count $flCount
Write-Host "forklift-bundle.jpg: $flMB MB" -ForegroundColor Green
# =============================================
# SUMMARY
# =============================================
Write-Host ""
Write-Host "=== DONE ===" -ForegroundColor Green
Write-Host "vault-bundle.jpg: $vaultMB MB" -ForegroundColor Green
Write-Host "forklift-bundle.jpg: $flMB MB" -ForegroundColor Green
$totalMB = $vaultMB + $flMB
Write-Host "Total: $totalMB MB (was ~71 MB)" -ForegroundColor Green
Write-Host ""
Write-Host "Optimized frames saved in frames-opt/ and forklift-frames-opt/"
Write-Host "Bundle files saved in frames/ and forklift-frames/"
Write-Host "You can delete the -opt folders after confirming everything works."