peixue-dev/Archive/[一次性]压缩图片-PowerShell-2026-01-31.ps1

101 lines
3.6 KiB
PowerShell
Raw Normal View History

2026-02-28 17:26:03 +08:00
# 小程序图片压缩脚本
# 使用 .NET 的 System.Drawing 压缩图片
Add-Type -AssemblyName System.Drawing
function Compress-Image {
param(
[string]$InputPath,
[int]$Quality = 50
)
try {
# 加载图片
$image = [System.Drawing.Image]::FromFile($InputPath)
# 创建临时文件
$tempPath = $InputPath + ".tmp"
# 设置JPEG编码器参数
$encoder = [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, $Quality)
# 获取原始大小
$originalSize = (Get-Item $InputPath).Length / 1KB
# 保存压缩后的图片
$image.Save($tempPath, $encoder, $encoderParams)
$image.Dispose()
# 获取压缩后大小
$compressedSize = (Get-Item $tempPath).Length / 1KB
# 替换原文件
Remove-Item $InputPath -Force
Rename-Item $tempPath $InputPath
$saved = $originalSize - $compressedSize
$percent = [math]::Round(($saved / $originalSize) * 100, 1)
Write-Host "$([System.IO.Path]::GetFileName($InputPath)): $([math]::Round($originalSize, 1))KB -> $([math]::Round($compressedSize, 1))KB (减少 $percent%)" -ForegroundColor Green
return $true
}
catch {
Write-Host "✗ 压缩失败 $InputPath : $_" -ForegroundColor Red
return $false
}
}
# 主程序
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "开始压缩小程序图片..." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$staticDir = "peidu\uniapp\src\static"
$totalOriginal = 0
$totalCompressed = 0
$successCount = 0
# 压缩 banner 目录
Write-Host "处理目录: banner/" -ForegroundColor Yellow
Write-Host "----------------------------------------"
Get-ChildItem "$staticDir\banner\*.jpg" | ForEach-Object {
$originalSize = $_.Length / 1KB
$totalOriginal += $originalSize
if (Compress-Image -InputPath $_.FullName -Quality 50) {
$compressedSize = (Get-Item $_.FullName).Length / 1KB
$totalCompressed += $compressedSize
$successCount++
}
}
Write-Host ""
# 压缩 button 目录
Write-Host "处理目录: button/" -ForegroundColor Yellow
Write-Host "----------------------------------------"
Get-ChildItem "$staticDir\button\*.jpg" | ForEach-Object {
$originalSize = $_.Length / 1KB
$totalOriginal += $originalSize
if (Compress-Image -InputPath $_.FullName -Quality 50) {
$compressedSize = (Get-Item $_.FullName).Length / 1KB
$totalCompressed += $compressedSize
$successCount++
}
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "压缩完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "成功压缩: $successCount 个文件"
Write-Host "原始总大小: $([math]::Round($totalOriginal, 1))KB"
Write-Host "压缩后总大小: $([math]::Round($totalCompressed, 1))KB"
Write-Host "节省空间: $([math]::Round($totalOriginal - $totalCompressed, 1))KB ($([math]::Round((($totalOriginal - $totalCompressed) / $totalOriginal) * 100, 1))%)"
Write-Host "========================================" -ForegroundColor Cyan