Created
June 30, 2017 04:07
-
-
Save mvadu/89b5232087cae3c8e28911b0c93e4d65 to your computer and use it in GitHub Desktop.
Fetch images from various Web URLs and resize and save
This file contains hidden or 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 ( | |
[ValidateNotNullOrEmpty()] | |
[string] $ConfigPath = ".\RefreshWebCache.json" | |
) | |
begin{ | |
Write-Host "$([datetime]::now.tostring()) Starting $(split-path -Leaf $MyInvocation.MyCommand.Definition ) process" | |
Add-Type -AssemblyName System.Drawing | |
$startTime = [System.DateTime]::Now | |
$pwd = split-path -parent $MyInvocation.MyCommand.Definition | |
if (!(Test-Path $ConfigPath)) { | |
throw "Error: The Configuration was not found at $ConfigPath!!" | |
Exit | |
} | |
$config = Get-Content -Raw -Path $ConfigPath | ConvertFrom-Json | |
} | |
process{ | |
foreach($img in $config.Images){ | |
$temp = [System.IO.Path]::GetTempFileName() | |
Invoke-WebRequest -Method Get -Uri $img.Url -OutFile $temp | |
try{ | |
[System.Drawing.Image] $fullSizeImg = [System.Drawing.Image]::FromFile($temp) | |
$newImage = New-Object System.Drawing.Bitmap([int]$img.Width, [int]$img.Height) | |
$gr = [System.Drawing.Graphics]::FromImage($newImage) | |
$gr.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality | |
$gr.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$gr.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality | |
$gr.DrawImage($fullSizeImg, -[int]$img.Left, -[int]$img.Top, [int]$img.Width+[int]$img.Left, [int]$img.Height+[int]$img.Top) | |
#$resized = $fullSizeImg.GetThumbnailImage($img.Width,$img.Height,$null,[System.IntPtr]::Zero) | |
$img.Path = join-path (resolve-path (split-path $img.Path -parent)) (split-path $img.path -leaf) | |
$newImage.Save($img.Path) | |
} | |
catch{Write-Host $_.Exception.Message} | |
finally{ | |
$fullSizeImg.Dispose() | |
#$resized.Dispose() | |
$gr.Dispose() | |
$newImage.Dispose() | |
Remove-Item $temp -Force | |
} | |
} | |
} | |
end{ | |
Write-Host "$([datetime]::now.tostring()) Done" | |
} |
This file contains hidden or 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
{ "Images": | |
[ | |
{ | |
"Path" : ".\\adystech.png", | |
"Url" : "https://avatars1.githubusercontent.com/u/11991955", | |
"Top" : "100", | |
"Left" : "0", | |
"Width" : "469", | |
"Height" : "402" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment