[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $true)] [string]$OutDir, [ValidateRange(1, 500)] [int]$BorderWidth = 24, [string]$Color = '#ffffff', [ValidateRange(1, 100)] [int]$JpegQuality = 92, [switch]$Recurse, [switch]$Force ) $ErrorActionPreference = 'Stop' Add-Type -AssemblyName System.Drawing function Save-Jpeg { param( [System.Drawing.Bitmap]$Bitmap, [string]$Target, [int]$Quality ) $encoder = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' } | Select-Object -First 1 $parameters = New-Object System.Drawing.Imaging.EncoderParameters(1) $parameter = New-Object System.Drawing.Imaging.EncoderParameter( [System.Drawing.Imaging.Encoder]::Quality, [long]$Quality ) $parameters.Param[0] = $parameter $Bitmap.Save($Target, $encoder, $parameters) $parameters.Dispose() } try { $borderColor = [System.Drawing.ColorTranslator]::FromHtml($Color) } catch { throw "Неверный цвет: $Color. Используйте #rrggbb или название цвета." } if ($borderColor.IsEmpty) { throw "Неверный цвет: $Color. Используйте #rrggbb или название цвета." } $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path if (Test-Path -LiteralPath $resolved -PathType Leaf) { $root = Split-Path -Parent $resolved $files = @(Get-Item -LiteralPath $resolved) } elseif (Test-Path -LiteralPath $resolved -PathType Container) { $root = $resolved $files = @(Get-ChildItem -LiteralPath $resolved -File -Recurse:$Recurse -ErrorAction SilentlyContinue) } else { throw "Путь не найден: $resolved" } $allowed = @('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff') $outRoot = [System.IO.Path]::GetFullPath($OutDir) if (-not (Test-Path -LiteralPath $outRoot)) { New-Item -ItemType Directory -Path $outRoot -Force | Out-Null } $done = 0 $skipped = 0 foreach ($file in $files) { $extension = $file.Extension.ToLowerInvariant() if ($extension -notin $allowed) { continue } $relative = $file.FullName.Substring($root.Length) -replace '^[\\/]+', '' $target = Join-Path $outRoot $relative if ((Test-Path -LiteralPath $target) -and -not $Force) { Write-Host "Пропуск: $target" $skipped++ continue } $parent = Split-Path -Parent $target if (-not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null } $image = [System.Drawing.Image]::FromFile($file.FullName) try { $orientation = 1 try { $orientation = [int]$image.GetPropertyItem(274).Value[0] } catch { $orientation = 1 } switch ($orientation) { 2 { $image.RotateFlip([System.Drawing.RotateFlipType]::RotateNoneFlipX) } 3 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate180FlipNone) } 4 { $image.RotateFlip([System.Drawing.RotateFlipType]::RotateNoneFlipY) } 5 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate90FlipX) } 6 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate90FlipNone) } 7 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate270FlipX) } 8 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate270FlipNone) } } if ($image.PropertyIdList -contains 274) { $image.RemovePropertyItem(274) } $canvasWidth = $image.Width + 2 * $BorderWidth $canvasHeight = $image.Height + 2 * $BorderWidth $pixelFormat = if ($extension -eq '.png') { [System.Drawing.Imaging.PixelFormat]::Format32bppArgb } else { [System.Drawing.Imaging.PixelFormat]::Format24bppRgb } $canvas = New-Object System.Drawing.Bitmap($canvasWidth, $canvasHeight, $pixelFormat) try { $graphics = [System.Drawing.Graphics]::FromImage($canvas) try { $graphics.Clear($borderColor) $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $graphics.DrawImage($image, $BorderWidth, $BorderWidth, $image.Width, $image.Height) } finally { $graphics.Dispose() } switch ($extension) { '.jpg' { Save-Jpeg -Bitmap $canvas -Target $target -Quality $JpegQuality } '.jpeg' { Save-Jpeg -Bitmap $canvas -Target $target -Quality $JpegQuality } '.png' { $canvas.Save($target, [System.Drawing.Imaging.ImageFormat]::Png) } '.bmp' { $canvas.Save($target, [System.Drawing.Imaging.ImageFormat]::Bmp) } '.tif' { $canvas.Save($target, [System.Drawing.Imaging.ImageFormat]::Tiff) } '.tiff' { $canvas.Save($target, [System.Drawing.Imaging.ImageFormat]::Tiff) } } } finally { $canvas.Dispose() } } finally { $image.Dispose() } Write-Host "Готово: $relative" $done++ } Write-Host "Итог: $done готово, $skipped пропущено"