Reputation: 616
I have a power shell script to resize some Images. Now I want to handover some parameter from one script to the other to resize all images within a certain folder. What am I doing wrong:
Param ( [Parameter(Mandatory=$True)] [ValidateNotNull()] $imageSource,
[Parameter(Mandatory=$True)] [ValidateNotNull()] $imageTarget,
[Parameter(Mandatory=$true)][ValidateNotNull()] $quality,
[Parameter(Mandatory=$true)][ValidateNotNull()] $canvasWidth,
[Parameter(Mandatory=$true)][ValidateNotNull()] $canvasHeight
)
if (!(Test-Path $imageSource)){throw( "Cannot find the source image")}
if(!([System.IO.Path]::IsPathRooted($imageSource))){throw("please enter a full path for your source path")}
if(!([System.IO.Path]::IsPathRooted($imageTarget))){throw("please enter a full path for your target path")}
if(!([System.IO.Path]::IsPathRooted($canvasWidth))){throw("please enter your maximum width")}
if(!([System.IO.Path]::IsPathRooted($canvasHeight))){throw("please enter your maximum height")}
if ($quality -lt 0 -or $quality -gt 100){throw( "quality must be between 0 and 100.")}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$bmp = [System.Drawing.Image]::FromFile($imageSource)
#hardcoded canvas size...
#$canvasWidth = 5
#$canvasHeight = 5
#Encoder parameter for image quality
$myEncoder = [System.Drawing.Imaging.Encoder]::Quality
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, $quality)
# get codec
$myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|where {$_.MimeType -eq 'image/jpeg'}
#compute the final ratio to use
$ratioX = $canvasWidth / $bmp.Width;
$ratioY = $canvasHeight / $bmp.Height;
$ratio = $ratioY
if($ratioX -le $ratioY){
$ratio = $ratioX
}
#create resized bitmap
$newWidth = [int] ($bmp.Width*$ratio)
$newHeight = [int] ($bmp.Height*$ratio)
$bmpResized = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
$graph = [System.Drawing.Graphics]::FromImage($bmpResized)
$graph.Clear([System.Drawing.Color]::White)
$graph.DrawImage($bmp,0,0 , $newWidth, $newHeight)
#save to file
$bmpResized.Save($imageTarget,$myImageCodecInfo, $($encoderParams))
$bmpResized.Dispose()
$bmp.Dispose()
The Script I use to execute my script:
Get-ChildItem .\img -Recurse -Include *.png | Foreach-Object{
$newName = $_.FullName.Substring(0, $_.FullName.Length - 4) + "_resized.png"
.\ImageResize.ps1 $_.FullName $newName 100 3 16
}
This is the error that I'm getting:
> Get-ChildItem : please enter your maximum width In
> C:\Users\me\Desktop\Scripte\Image_Resize\Image.ps1:1 Zeichen:1
> + Get-ChildItem .\img -Recurse -Include *.png | Foreach-Object{
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : OperationStopped: (please enter your maximum width:String) [Get-ChildItem], RuntimeException
> + FullyQualifiedErrorId : please enter your maximum width,Microsoft.PowerShell.Commands.GetChildItemCommand
Upvotes: 1
Views: 567
Reputation: 174835
As noted in the comments, [System.IO.Path]::IsPathRooted($canvasWidth)
makes zero sense. The method is intended to receive a string argument representing a path.
Stop calling [System.IO.Path]::IsPathRooted()
with numerical arguments like $canvasWidth
PowerShell also supports validation attributes to simplify input validation, no need for you to do that inline, manually:
function Test-ParamValidation {
param(
[Parameter(Mandatory=$true)]
[ValidateScript({[System.IO.Path]::IsPathRooted($_)})]
[string]$Path,
[Parameter(Mandatory=$true)]
[ValidateRange(1,500)]
[int]$Width
)
}
Check out the Scripting Guys post Simplify Your PowerShell Script with Parameter Validation
Upvotes: 3