Last active
August 29, 2015 14:03
-
-
Save sayedihashimi/d6a72c096a55b1d60ebb to your computer and use it in GitHub Desktop.
Script which can be used to optimize images. All you need to do is download the script and call it. The script will download the files it needs to run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function OptimizeImages{ | |
[cmdletbinding()] | |
param( | |
$folder, | |
$force = $false, | |
$customTemp = "$env:LocalAppData\CustomPublish\", | |
$imgOptUrl = 'https://raw.githubusercontent.com/ligershark/AzureJobs/master/ImageCompressor.Job/optimize-images.ps1' | |
) | |
process{ | |
if(!(Test-Path $customTemp)){New-Item $customTemp -ItemType Directory} | |
$imgOptPath = (Join-Path $customTemp 'optimize-images.ps1') | |
if(!(Test-Path $imgOptPath)){ | |
# download the file | |
'Downloading optimize-images.ps1' | Write-Verbose | |
(New-Object System.Net.WebClient).DownloadFile($imgOptUrl, $imgOptPath) | |
} | |
&$imgOptPath $folder $force | |
} | |
} | |
$customTemp = "$env:LocalAppData\CustomPublish\" | |
$imgOptPath = Join-Path $customTemp 'optimize-images.ps1' | |
$imgOptUrl = 'https://raw.githubusercontent.com/ligershark/AzureJobs/master/ImageCompressor.Job/optimize-images.ps1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[cmdletbinding()] | |
param( | |
$folderToOptimize = ($pwd), | |
[switch] | |
$force = $false, | |
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"), | |
$nugetDownloadUrl = 'http://nuget.org/nuget.exe' | |
) | |
<# | |
.SYNOPSIS | |
If nuget is in the tools | |
folder then it will be downloaded there. | |
#> | |
function Get-Nuget(){ | |
[cmdletbinding()] | |
param( | |
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"), | |
$nugetDownloadUrl = 'http://nuget.org/nuget.exe' | |
) | |
process{ | |
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe | |
if(!(Test-Path $nugetDestPath)){ | |
'Downloading nuget.exe' | Write-Verbose | |
(New-Object System.Net.WebClient).DownloadFile($nugetDownloadUrl, $nugetDestPath) | |
# double check that is was written to disk | |
if(!(Test-Path $nugetDestPath)){ | |
throw 'unable to download nuget' | |
} | |
} | |
# return the path of the file | |
$nugetDestPath | |
} | |
} | |
<# | |
.SYNOPSIS | |
If the image optimizer in the .ools | |
folder then it will be downloaded there. | |
#> | |
function GetImageOptimizer(){ | |
[cmdletbinding()] | |
param( | |
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"), | |
$nugetDownloadUrl = 'http://nuget.org/nuget.exe' | |
) | |
process{ | |
if(!(Test-Path $toolsDir)){ | |
New-Item $toolsDir -ItemType Directory | Out-Null | |
} | |
$imgOptimizer = (Get-ChildItem -Path $toolsDir -Include 'ImageCompressor.Job.exe' -Recurse) | |
if(!$imgOptimizer){ | |
'Downloading image optimizer to the .tools folder' | Write-Verbose | |
# nuget install AzureImageOptimizer -Prerelease -OutputDirectory C:\temp\nuget\out\ | |
$cmdArgs = @('install','AzureImageOptimizer','-Prerelease','-OutputDirectory',(Resolve-Path $toolsDir).ToString()) | |
'Calling nuget to install image optimzer with the following args. [{0}]' -f ($cmdArgs -join ' ') | Write-Verbose | |
&(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null | |
} | |
$imgOptimizer = Get-ChildItem -Path $toolsDir -Include 'ImageCompressor.Job.exe' -Recurse | select -first 1 | |
if(!$imgOptimizer){ throw 'Image optimizer not found' } | |
$imgOptimizer | |
} | |
} | |
function OptimizeImages(){ | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory=$true,Position=0)] | |
$dir, | |
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"), | |
$nugetDownloadUrl = 'http://nuget.org/nuget.exe' | |
) | |
process{ | |
[string]$imgOptExe = (GetImageOptimizer -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) | |
[string]$folderToOptimize = (Resolve-path $dir) | |
'Starting image optimizer on folder [{0}]' -f $dir | Write-Output | |
# .\.tools\AzureImageOptimizer.0.0.10-beta\tools\ImageCompressor.Job.exe --dir M:\temp\images\opt\to-optimize | |
$cmdArgs = @('--dir', ('{0}\' -f (get-item $folderToOptimize).FullName)) | |
if($force){ | |
$cmdArgs += '--force' | |
} | |
'Calling img optimizer with the following args [{0} {1}]' -f $imgOptExe, ($cmdArgs -join ' ') | Write-Output | |
&$imgOptExe $cmdArgs | |
'Images optimized' | Write-Output | |
} | |
} | |
OptimizeImages -dir $folderToOptimize -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment