-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResizeImageModule.psm1
164 lines (131 loc) · 5.62 KB
/
ResizeImageModule.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function Resize-Image {
<#
.SYNOPSIS
Resize-Image resizes an image file.
.DESCRIPTION
This function uses the native .NET API to resize an image file and save it to a file.
It supports the following image formats: BMP, GIF, JPEG, PNG, TIFF
.PARAMETER InputFile
Type [string]
The parameter InputFile is used to define the value of image name or path to resize.
.PARAMETER OutputFile
Type [string]
The parameter OutputFile is used to define the value of output image resize.
.PARAMETER Width
Type [int32]
The parameter Width is used to define the value of new width to image.
.PARAMETER Height
Type [int32]
The parameter Height is used to define the value of new height to image.
.PARAMETER ProportionalResize
Type [bool]
The optional parameter ProportionalResize is used to define if execute proportional resize.
.EXAMPLE
Resize-Image -InputFile "C:/image.png" -OutputFile "C:/image2.png" -Width 300 -Height 300
.NOTES
Author: Ronildo Souza
Last Edit: 2018-10-09
Version 1.0.0 - initial release
Version 1.0.1 - add proportional resize
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$InputFile,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$OutputFile,
[Parameter(Mandatory = $true)]
[int32]$Width,
[Parameter(Mandatory = $true)]
[int32]$Height,
[Parameter(Mandatory = $false)]
[bool]$ProportionalResize = $true)
# Add assemblies
Add-Type -AssemblyName System
Add-Type -AssemblyName System.Drawing
$image = [System.Drawing.Image]::FromFile((Get-Item $InputFile))
$ratioX = $Width / $image.Width;
$ratioY = $Height / $image.Height;
$ratio = [System.Math]::Min($ratioX, $ratioY);
[int32]$newWidth = If ($ProportionalResize) { $image.Width * $ratio } Else { $Width }
[int32]$newHeight = If ($ProportionalResize) { $image.Height * $ratio } Else { $Height }
$destImage = New-Object System.Drawing.Bitmap($newWidth, $newHeight)
# Draw new image on the empty canvas
$graphics = [System.Drawing.Graphics]::FromImage($destImage)
$graphics.DrawImage($image, 0, 0, $newWidth, $newHeight)
$graphics.Dispose()
# Save the image
$destImage.Save($OutputFile)
# Close source file
$image.Dispose()
}
function Resize-ImagesInFolder {
<#
.SYNOPSIS
Resize-ImagesInFolder resizes an image files in folder.
.DESCRIPTION
This function uses the native .NET API to resize an image file and save it to a file.
It supports the following image formats: BMP, GIF, JPEG, PNG, TIFF
.PARAMETER Width
Type [int32]
The parameter Width is used to define the value of new width to image.
.PARAMETER Height
Type [int32]
The parameter Height is used to define the value of new height to image.
.PARAMETER FolderPath
Type [string]
The optional parameter FolderPath is used to define the value of image files folder path.
.PARAMETER ProportionalResize
Type [bool]
The optional parameter ProportionalResize is used to define if execute proportional resize.
.EXAMPLE
Resize-ImagesInFolder -Width 300 -Height 300 [-FolderPath]
.NOTES
Author: Ronildo Souza
Last Edit: 2018-10-09
Version 1.0.0 - initial release
Version 1.0.1 - add proportional resize
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)][int32]$Width,
[Parameter(Mandatory = $true)][int32]$Height,
[Parameter(Mandatory = $false)]
[string]$FolderPath = "",
[Parameter(Mandatory = $false)]
[bool]$ProportionalResize = $true)
$imageList = Get-ChildItem -Path $FolderPath | Where-Object {$_.Extension -in (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tiff")}
if ($imageList.Length -gt 0) {
foreach ($image in $imageList) {
$pathWindowsTemp = $env:SystemRoot + "\temp\pictures-rim_ps\"
$extension = $image.Extension
$imageInputFullName = $image.FullName
$imageInputBaseName = $image.BaseName
$imageOutput = $FolderPath + "\" + $imageInputBaseName + "_RESIZE-RIM_PS" + $extension
$imageInput = $pathWindowsTemp + $imageInputBaseName + $extension
$imageNameBKP = $FolderPath + "\" + $imageInputBaseName + $extension + ".bkp"
# Create temp folder and image file copy
New-Item -ItemType Directory -Path $pathWindowsTemp -Force
Copy-Item $imageInputFullName $pathWindowsTemp -Force
# Create backup image file
if (!(Test-Path "$imageNameBKP" -PathType Leaf)) {
Rename-Item -Path "$imageInputFullName" -NewName "$imageNameBKP" -Force
}
# Resize the current image file of loop
Resize-Image -InputFile "$imageInput" -OutputFile "$imageOutput" -Width $Width -Height $Height -ProportionalResize $ProportionalResize
# "Rename" item with override
$Destination = Join-Path -Path $image.Directory.FullName -ChildPath "$imageInputBaseName$extension"
Move-Item -Path $imageOutput -Destination $Destination -Force
Write-Output $image.Name
}
Read-Host -Prompt "Success Resize!"
Exit-PSHostProcess
}
else {
Write-Error "*** Folder not contain image files! ***"
}
}
Export-ModuleMember -Function Resize-Image, `
Resize-ImagesInFolder