Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Resize an image
  4. .DESCRIPTION
  5. Resize an image based on a new given height or width or a single dimension and a maintain ratio flag.
  6. The execution of this CmdLet creates a new file named "OriginalName_resized" and maintains the original
  7. file extension
  8. .PARAMETER Width
  9. The new width of the image. Can be given alone with the MaintainRatio flag
  10. .PARAMETER Height
  11. The new height of the image. Can be given alone with the MaintainRatio flag
  12. .PARAMETER ImagePath
  13. The path to the image being resized
  14. .PARAMETER MaintainRatio
  15. Maintain the ratio of the image by setting either width or height. Setting both width and height and also this parameter
  16. results in an error
  17. .PARAMETER Percentage
  18. Resize the image *to* the size given in this parameter. It's imperative to know that this does not resize by the percentage but to the percentage of
  19. the image.
  20. .PARAMETER SmoothingMode
  21. Sets the smoothing mode. Default is HighQuality.
  22. .PARAMETER InterpolationMode
  23. Sets the interpolation mode. Default is HighQualityBicubic.
  24. .PARAMETER PixelOffsetMode
  25. Sets the pixel offset mode. Default is HighQuality.
  26. .EXAMPLE
  27. Resize-Image -Height 45 -Width 45 -ImagePath "Path/to/image.jpg"
  28. .EXAMPLE
  29. Resize-Image -Height 45 -MaintainRatio -ImagePath "Path/to/image.jpg"
  30. .EXAMPLE
  31. #Resize to 50% of the given image
  32. Resize-Image -Percentage 50 -ImagePath "Path/to/image.jpg"
  33. .NOTES
  34. Written By:
  35. Christopher Walker
  36. #>
  37. Function Resize-Image() {
  38. [CmdLetBinding(
  39. SupportsShouldProcess=$true,
  40. PositionalBinding=$false,
  41. ConfirmImpact="Medium",
  42. DefaultParameterSetName="Absolute"
  43. )]
  44. Param (
  45. [Parameter(Mandatory=$True)]
  46. [ValidateScript({
  47. $_ | ForEach-Object {
  48. Test-Path $_
  49. }
  50. })][String[]]$ImagePath,
  51. [Parameter(Mandatory=$False)][Switch]$MaintainRatio,
  52. [Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Height,
  53. [Parameter(Mandatory=$False, ParameterSetName="Absolute")][Int]$Width,
  54. [Parameter(Mandatory=$False, ParameterSetName="Percent")][Double]$Percentage,
  55. [Parameter(Mandatory=$False)][System.Drawing.Drawing2D.SmoothingMode]$SmoothingMode = "HighQuality",
  56. [Parameter(Mandatory=$False)][System.Drawing.Drawing2D.InterpolationMode]$InterpolationMode = "HighQualityBicubic",
  57. [Parameter(Mandatory=$False)][System.Drawing.Drawing2D.PixelOffsetMode]$PixelOffsetMode = "HighQuality",
  58. [Parameter(Mandatory=$False)][String]$NameModifier = "resized"
  59. )
  60. Begin {
  61. If ($Width -and $Height -and $MaintainRatio) {
  62. Throw "Absolute Width and Height cannot be given with the MaintainRatio parameter."
  63. }
  64.  
  65. If (($Width -xor $Height) -and (-not $MaintainRatio)) {
  66. Throw "MaintainRatio must be set with incomplete size parameters (Missing height or width without MaintainRatio)"
  67. }
  68.  
  69. If ($Percentage -and $MaintainRatio) {
  70. Write-Warning "The MaintainRatio flag while using the Percentage parameter does nothing"
  71. }
  72. }
  73. Process {
  74. ForEach ($Image in $ImagePath) {
  75. $Path = (Resolve-Path $Image).Path
  76. $Dot = $Path.LastIndexOf(".")
  77.  
  78. #Add name modifier (OriginalName_{$NameModifier}.jpg)
  79. $OutputPath = $Path.Substring(0,$Dot) + "_" + $NameModifier + $Path.Substring($Dot,$Path.Length - $Dot)
  80.  
  81. $OldImage = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Path
  82. # Grab these for use in calculations below.
  83. $OldHeight = $OldImage.Height
  84. $OldWidth = $OldImage.Width
  85.  
  86. If ($MaintainRatio) {
  87. $OldHeight = $OldImage.Height
  88. $OldWidth = $OldImage.Width
  89. If ($Height) {
  90. $Width = $OldWidth / $OldHeight * $Height
  91. }
  92. If ($Width) {
  93. $Height = $OldHeight / $OldWidth * $Width
  94. }
  95. }
  96.  
  97. If ($Percentage) {
  98. $Product = ($Percentage / 100)
  99. $Height = $OldHeight * $Product
  100. $Width = $OldWidth * $Product
  101. }
  102.  
  103. $Bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $Width, $Height
  104. $NewImage = [System.Drawing.Graphics]::FromImage($Bitmap)
  105.  
  106. #Retrieving the best quality possible
  107. $NewImage.SmoothingMode = $SmoothingMode
  108. $NewImage.InterpolationMode = $InterpolationMode
  109. $NewImage.PixelOffsetMode = $PixelOffsetMode
  110. $NewImage.DrawImage($OldImage, $(New-Object -TypeName System.Drawing.Rectangle -ArgumentList 0, 0, $Width, $Height))
  111.  
  112. If ($PSCmdlet.ShouldProcess("Resized image based on $Path", "save to $OutputPath")) {
  113. $Bitmap.Save($OutputPath)
  114. }
  115.  
  116. $Bitmap.Dispose()
  117. $NewImage.Dispose()
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment