Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Friendly Configuration
- $TMP_DIR = "$env:TEMP\jpeg_optimizer"
- $QUALITY = 100 # Maximum quality setting
- $PRESERVE_METADATA = $true # Keep EXIF/GPS data by default
- $KEEP_ORIGINAL = $true # Safety default for new users
- # Visual Feedback
- $SUCCESS = "✅"
- $INFO = "ℹ️"
- $WARNING = "⚠️"
- $ERROR = "❌"
- # Setup environment
- if (Test-Path $TMP_DIR) {
- Remove-Item -Recurse -Force $TMP_DIR
- }
- New-Item -ItemType Directory -Force -Path $TMP_DIR
- # Check for mozjpeg
- if (-not (Get-Command mozjpeg -ErrorAction SilentlyContinue)) {
- Write-Host "$WARNING 'mozjpeg' not found. Install it first."
- exit 1
- }
- # Function to test compression with different parameters
- function Test-Compression {
- param (
- [string]$input
- )
- # Temp file to store results
- $R0 = [System.IO.Path]::GetTempFileName()
- $R1 = [System.IO.Path]::GetTempFileName()
- $best_size = [Int64]::MaxValue
- $best_params = ""
- # List of parameter sets to test
- $param_sets = @(
- "-dct float -quant-table 1 -nojfif -dc-scan-opt 2",
- "-dct float -quant-table 2 -nojfif -dc-scan-opt 2",
- "-dct float -quant-table 3 -nojfif -dc-scan-opt 2",
- "-dct float -tune-ms-ssim -nojfif -dc-scan-opt 2",
- "-dct float -tune-ms-ssim -quant-table 3 -nojfif -dc-scan-opt 2",
- "-dct float -tune-ssim -nojfif -dc-scan-opt 2",
- "-dct float -tune-ssim -quant-table 3 -nojfif -dc-scan-opt 2"
- )
- # Test each parameter set
- foreach ($param in $param_sets) {
- $cmd = "mozjpeg -memdst $param `"$input`" > $R0 2>&1"
- Invoke-Expression $cmd
- $compressed_size = (Get-Item $R0).Length
- if ($compressed_size -lt $best_size) {
- $best_size = $compressed_size
- $best_params = $param
- }
- }
- # Clean up temp files
- Remove-Item $R0
- return $best_params
- }
- # Function to optimize a single image
- function Optimize-Image {
- param (
- [string]$input
- )
- Write-Host "$INFO Processing: $(Split-Path -Leaf $input)"
- # Get best compression parameters
- $best_params = Test-Compression -input $input
- # Run compression with best parameters
- $output = "$input.opti.jpg"
- $cmd = "mozjpeg -memdst $best_params `"$input`" > `"$output`""
- Invoke-Expression $cmd
- # Compare sizes
- $original_size = (Get-Item $input).length
- $new_size = (Get-Item $output).length
- if ($new_size -lt $original_size) {
- Write-Host "$SUCCESS Size reduced: $([math]::Round($original_size / 1KB))KB → $([math]::Round($new_size / 1KB))KB"
- # Create safe backup with original extension if required
- if ($KEEP_ORIGINAL) {
- $optimized_file = "$($input.Substring(0, $input.LastIndexOf('.')))_optimized$($input.Substring($input.LastIndexOf('.')))"
- Move-Item $output $optimized_file
- } else {
- Move-Item $output $input
- }
- } else {
- Write-Host "$WARNING No optimization needed - file already optimal"
- Remove-Item $output
- }
- }
- # Argument parsing (simplified for PowerShell)
- $FILES = @()
- foreach ($arg in $args) {
- if (Test-Path $arg) {
- $FILES += $arg
- }
- }
- # Main processing
- if ($FILES.Count -eq 0) {
- Write-Host "$WARNING No valid files/folders provided"
- exit 1
- }
- Write-Host "$INFO Found $($FILES.Count) files to process"
- foreach ($file in $FILES) {
- Optimize-Image -input $file
- }
- Write-Host "$SUCCESS Optimization complete!"
- # Ask user if they want to replace original files
- $response = Read-Host "Replace original jpg or png files with compressed .opti.jpg backups? Press Y for YES or N for NO"
- if ($response -match '^[yY]$') {
- Get-ChildItem -Recurse -Filter '*.opti.jpg' | ForEach-Object {
- $original = $_.FullName.Substring(0, $_.FullName.LastIndexOf('.'))
- Move-Item $_.FullName -Destination $original -Force
- Write-Host "$SUCCESS Replaced: $original"
- }
- Write-Host "Original files replaced."
- } else {
- Write-Host "Original and compressed files were preserved. Compressed files have .opti.jpg suffix."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement