Advertisement
Guest User

Untitled

a guest
Mar 16th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. # Friendly Configuration
  2. $TMP_DIR = "$env:TEMP\jpeg_optimizer"
  3. $QUALITY = 100 # Maximum quality setting
  4. $PRESERVE_METADATA = $true # Keep EXIF/GPS data by default
  5. $KEEP_ORIGINAL = $true # Safety default for new users
  6.  
  7. # Visual Feedback
  8. $SUCCESS = "✅"
  9. $INFO = "ℹ️"
  10. $WARNING = "⚠️"
  11. $ERROR = "❌"
  12.  
  13. # Setup environment
  14. if (Test-Path $TMP_DIR) {
  15. Remove-Item -Recurse -Force $TMP_DIR
  16. }
  17. New-Item -ItemType Directory -Force -Path $TMP_DIR
  18.  
  19. # Check for mozjpeg
  20. if (-not (Get-Command mozjpeg -ErrorAction SilentlyContinue)) {
  21. Write-Host "$WARNING 'mozjpeg' not found. Install it first."
  22. exit 1
  23. }
  24.  
  25. # Function to test compression with different parameters
  26. function Test-Compression {
  27. param (
  28. [string]$input
  29. )
  30.  
  31. # Temp file to store results
  32. $R0 = [System.IO.Path]::GetTempFileName()
  33. $R1 = [System.IO.Path]::GetTempFileName()
  34.  
  35. $best_size = [Int64]::MaxValue
  36. $best_params = ""
  37.  
  38. # List of parameter sets to test
  39. $param_sets = @(
  40. "-dct float -quant-table 1 -nojfif -dc-scan-opt 2",
  41. "-dct float -quant-table 2 -nojfif -dc-scan-opt 2",
  42. "-dct float -quant-table 3 -nojfif -dc-scan-opt 2",
  43. "-dct float -tune-ms-ssim -nojfif -dc-scan-opt 2",
  44. "-dct float -tune-ms-ssim -quant-table 3 -nojfif -dc-scan-opt 2",
  45. "-dct float -tune-ssim -nojfif -dc-scan-opt 2",
  46. "-dct float -tune-ssim -quant-table 3 -nojfif -dc-scan-opt 2"
  47. )
  48.  
  49. # Test each parameter set
  50. foreach ($param in $param_sets) {
  51. $cmd = "mozjpeg -memdst $param `"$input`" > $R0 2>&1"
  52. Invoke-Expression $cmd
  53.  
  54. $compressed_size = (Get-Item $R0).Length
  55.  
  56. if ($compressed_size -lt $best_size) {
  57. $best_size = $compressed_size
  58. $best_params = $param
  59. }
  60. }
  61.  
  62. # Clean up temp files
  63. Remove-Item $R0
  64.  
  65. return $best_params
  66. }
  67.  
  68. # Function to optimize a single image
  69. function Optimize-Image {
  70. param (
  71. [string]$input
  72. )
  73.  
  74. Write-Host "$INFO Processing: $(Split-Path -Leaf $input)"
  75.  
  76. # Get best compression parameters
  77. $best_params = Test-Compression -input $input
  78.  
  79. # Run compression with best parameters
  80. $output = "$input.opti.jpg"
  81. $cmd = "mozjpeg -memdst $best_params `"$input`" > `"$output`""
  82. Invoke-Expression $cmd
  83.  
  84. # Compare sizes
  85. $original_size = (Get-Item $input).length
  86. $new_size = (Get-Item $output).length
  87.  
  88. if ($new_size -lt $original_size) {
  89. Write-Host "$SUCCESS Size reduced: $([math]::Round($original_size / 1KB))KB → $([math]::Round($new_size / 1KB))KB"
  90.  
  91. # Create safe backup with original extension if required
  92. if ($KEEP_ORIGINAL) {
  93. $optimized_file = "$($input.Substring(0, $input.LastIndexOf('.')))_optimized$($input.Substring($input.LastIndexOf('.')))"
  94. Move-Item $output $optimized_file
  95. } else {
  96. Move-Item $output $input
  97. }
  98. } else {
  99. Write-Host "$WARNING No optimization needed - file already optimal"
  100. Remove-Item $output
  101. }
  102. }
  103.  
  104. # Argument parsing (simplified for PowerShell)
  105. $FILES = @()
  106. foreach ($arg in $args) {
  107. if (Test-Path $arg) {
  108. $FILES += $arg
  109. }
  110. }
  111.  
  112. # Main processing
  113. if ($FILES.Count -eq 0) {
  114. Write-Host "$WARNING No valid files/folders provided"
  115. exit 1
  116. }
  117.  
  118. Write-Host "$INFO Found $($FILES.Count) files to process"
  119. foreach ($file in $FILES) {
  120. Optimize-Image -input $file
  121. }
  122.  
  123. Write-Host "$SUCCESS Optimization complete!"
  124.  
  125. # Ask user if they want to replace original files
  126. $response = Read-Host "Replace original jpg or png files with compressed .opti.jpg backups? Press Y for YES or N for NO"
  127. if ($response -match '^[yY]$') {
  128. Get-ChildItem -Recurse -Filter '*.opti.jpg' | ForEach-Object {
  129. $original = $_.FullName.Substring(0, $_.FullName.LastIndexOf('.'))
  130. Move-Item $_.FullName -Destination $original -Force
  131. Write-Host "$SUCCESS Replaced: $original"
  132. }
  133. Write-Host "Original files replaced."
  134. } else {
  135. Write-Host "Original and compressed files were preserved. Compressed files have .opti.jpg suffix."
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement