Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
Last active May 28, 2023 16:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save craigmaslowski/b31c4c71b95463abe7a891f4a0a0c1a6 to your computer and use it in GitHub Desktop.
Save craigmaslowski/b31c4c71b95463abe7a891f4a0a0c1a6 to your computer and use it in GitHub Desktop.
Powershell script to compress all jpg, gif, or png files recursively in the given path with ImageMagick
param([string]$path = ".\", [int]$minSize = 0, [switch]$jpg, [switch]$png, [switch]$gif, [switch]$verbose, [switch]$report)
function Get-Size
{
param([string]$pth)
"{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1mb) + " mb"
}
function Get-Size-Kb
{
param([string]$pth)
"{0:n2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1kb) + " kb"
}
function Compress-Images([string]$type) {
$params = switch ($type) {
"jpg" { "-compress jpeg -quality 82" }
"gif" { "-fuzz 10% -layers Optimize" }
"png" { "-depth 24 -define png:compression-filter=2 -define png:compression-level=9 -define png:compression-strategy=1" }
}
if ($report) {
echo ""
echo "Listing $type files that would be included for compression with params: $params"
} else {
echo ""
echo "Compressing $type files with parameters: $params"
}
Get-ChildItem $path -Recurse -Include "*.$type" |
Where-Object {
$_.Length/1kb -gt $minSize
} |
Sort-Object -Descending length |
ForEach-Object {
$file = $_.FullName
if ($report) {
$fSize = Get-Size-Kb($file)
echo "$file - $fSize"
} else {
if ($verbose) {
echo "Compressing $file"
$fileStartSize = Get-Size-Kb($file)
}
# compress image
if ($report -eq $False) {
iex "magick $file $params $file"
}
if ($verbose) {
$fileEndSize = Get-Size-Kb($file)
echo "Reduced from $fileStartSize to $fileEndSize"
}
}
}
}
# begin compression process
$startSize = Get-Size $path
echo "Compressing images greater than $minSize kb in $path"
echo "---"
# determine whether to compress specific image types or all
$compressAll = $false
if (-NOT $jpg -AND -NOT $png -AND -NOT $gif) {
$compressAll = $true
}
# compress, or skip, each image type as directed
if ($jpg -OR $compressAll) {
Compress-Images "jpg"
}
if ($gif -OR $compressAll) {
Compress-Images "gif"
}
if ($png -OR $compressAll) {
Compress-Images "png"
}
# echo completion and stats
$endSize = Get-Size $path
echo ""
echo "DONE"
echo "Starting sizes: $startSize"
echo "Ending sizes: $endSize"
@xploke
Copy link

xploke commented Oct 9, 2020

Hi,

If is possible can you explain me how works this scripts? I tried but have problem with term magick " is not recongnized as the name of a cmdlet"....

@craigmaslowski
Copy link
Author

craigmaslowski commented Oct 10, 2020

term magick " is not recongnized as the name of a cmdlet"....

@xploke This script uses the program ImageMagick. You need to have it installed for it to work.

https://imagemagick.org/

@pvachonspaq
Copy link

Thank you for your script.

I modified the code at line 36 to: "'" + $_.FullName + "'" for filename with space.

@Ingmar4ty
Copy link

DONE Starting sizes: 80.36 mb Ending sizes: 114.94 mb

:D:D:D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment