11Add-Type - AssemblyName System.Windows.Forms
22Add-Type - AssemblyName System.Drawing
33
4- # Function to append log messages to the log textbox
4+ # ---------- helper: log to textbox ----------
55function Add-Log {
66 param (
77 [string ]$Message ,
@@ -12,15 +12,15 @@ function Add-Log {
1212 $LogBox.ScrollToCaret ()
1313}
1414
15- # Create the main form
15+ # ---------- main form ----------
1616$form = New-Object System.Windows.Forms.Form
1717$form.Text = " Image Converter (PNG/JPG/AVIF -> WebP / AVIF)"
1818$form.StartPosition = " CenterScreen"
1919$form.Size = New-Object System.Drawing.Size(700 , 520 )
2020$form.FormBorderStyle = ' FixedDialog'
2121$form.MaximizeBox = $false
2222
23- # Folder selection controls
23+ # ---------- folder selection ----------
2424$labelFolder = New-Object System.Windows.Forms.Label
2525$labelFolder.Text = " Folder:"
2626$labelFolder.Location = New-Object System.Drawing.Point(15 , 20 )
@@ -46,7 +46,7 @@ $buttonBrowse.Add_Click({
4646 }
4747})
4848
49- # Quality input controls
49+ # ---------- quality ----------
5050$labelQuality = New-Object System.Windows.Forms.Label
5151$labelQuality.Text = " Quality (0-100):"
5252$labelQuality.Location = New-Object System.Drawing.Point(15 , 55 )
@@ -59,7 +59,7 @@ $textQuality.Size = New-Object System.Drawing.Size(50, 25)
5959$textQuality.Text = " 85"
6060$form.Controls.Add ($textQuality )
6161
62- # Output format controls
62+ # ---------- output format ----------
6363$labelFormat = New-Object System.Windows.Forms.Label
6464$labelFormat.Text = " Output format:"
6565$labelFormat.Location = New-Object System.Drawing.Point(200 , 55 )
@@ -72,33 +72,32 @@ $comboFormat.Size = New-Object System.Drawing.Size(80, 25)
7272$comboFormat.DropDownStyle = ' DropDownList'
7373[void ]$comboFormat.Items.Add (" webp" )
7474[void ]$comboFormat.Items.Add (" avif" )
75- $comboFormat.SelectedIndex = 0 # default: webp
75+ $comboFormat.SelectedIndex = 0
7676$form.Controls.Add ($comboFormat )
7777
78- # Checkbox: delete originals
78+ # ---------- checkboxes ----------
7979$checkDelete = New-Object System.Windows.Forms.CheckBox
8080$checkDelete.Text = " Delete original source files after conversion"
8181$checkDelete.Location = New-Object System.Drawing.Point(15 , 85 )
8282$checkDelete.AutoSize = $true
8383$checkDelete.Checked = $true
8484$form.Controls.Add ($checkDelete )
8585
86- # Checkbox: recursive processing
8786$checkRecursive = New-Object System.Windows.Forms.CheckBox
8887$checkRecursive.Text = " Include subfolders (recursive)"
8988$checkRecursive.Location = New-Object System.Drawing.Point(15 , 110 )
9089$checkRecursive.AutoSize = $true
9190$checkRecursive.Checked = $false
9291$form.Controls.Add ($checkRecursive )
9392
94- # Start conversion button
93+ # ---------- start button ----------
9594$buttonStart = New-Object System.Windows.Forms.Button
9695$buttonStart.Text = " Start conversion"
9796$buttonStart.Location = New-Object System.Drawing.Point(15 , 140 )
9897$buttonStart.Size = New-Object System.Drawing.Size(150 , 35 )
9998$form.Controls.Add ($buttonStart )
10099
101- # Log output textbox
100+ # ---------- log textbox ----------
102101$logBox = New-Object System.Windows.Forms.TextBox
103102$logBox.Location = New-Object System.Drawing.Point(15 , 190 )
104103$logBox.Size = New-Object System.Drawing.Size(645 , 270 )
@@ -108,68 +107,65 @@ $logBox.ReadOnly = $true
108107$logBox.Font = New-Object System.Drawing.Font(" Consolas" , 9 )
109108$form.Controls.Add ($logBox )
110109
111- # Click handler for start button
110+ # ---------- start button logic ----------
112111$buttonStart.Add_Click ({
113112 $folder = $textFolder.Text.Trim ()
114113 if ([string ]::IsNullOrWhiteSpace($folder ) -or -not (Test-Path $folder )) {
115114 [System.Windows.Forms.MessageBox ]::Show(" Please select a valid folder." , " Error" , " OK" , " Error" )
116115 return
117116 }
118117
119- # Check ImageMagick availability
118+ # Check ImageMagick
120119 $magickCmd = Get-Command magick - ErrorAction SilentlyContinue
121120 if (-not $magickCmd ) {
122121 [System.Windows.Forms.MessageBox ]::Show(
123- " ImageMagick 'magick' command not found in PATH.`r`n Please install ImageMagick and make sure 'magick' is available ." ,
122+ " ImageMagick 'magick' command not found in PATH.`r`n Please install ImageMagick." ,
124123 " ImageMagick not found" ,
125124 " OK" ,
126125 " Error"
127126 )
128127 return
129128 }
130129
131- # Parse quality
130+ # -------- FIXED QUALITY VALIDATION --------
132131 $qualityText = $textQuality.Text.Trim ()
133- if (-not [int ]::TryParse($qualityText , [ref ]([int ]$quality ))) {
134- [System.Windows.Forms.MessageBox ]::Show(" Quality must be an integer between 0 and 100." , " Error" , " OK" , " Error" )
132+
133+ [int ]$quality = 0
134+ if (-not [int ]::TryParse($qualityText , [ref ]$quality )) {
135+ [System.Windows.Forms.MessageBox ]::Show(
136+ " Quality must be an integer between 0 and 100." ,
137+ " Error" , " OK" , " Error"
138+ )
135139 return
136140 }
141+
137142 if ($quality -lt 0 -or $quality -gt 100 ) {
138- [System.Windows.Forms.MessageBox ]::Show(" Quality must be between 0 and 100." , " Error" , " OK" , " Error" )
143+ [System.Windows.Forms.MessageBox ]::Show(
144+ " Quality must be between 0 and 100." ,
145+ " Error" , " OK" , " Error"
146+ )
139147 return
140148 }
149+ # ------------------------------------------
141150
142- # Output format selection
143151 $format = $comboFormat.SelectedItem
144- if ([string ]::IsNullOrWhiteSpace($format )) {
145- [System.Windows.Forms.MessageBox ]::Show(" Please select an output format." , " Error" , " OK" , " Error" )
146- return
147- }
148-
149152 $deleteOriginals = $checkDelete.Checked
150153 $recursive = $checkRecursive.Checked
151154
152- # Reset log
153155 $logBox.Clear ()
154- Add-Log " Starting PNG/JPG/AVIF -> $format conversion..." $logBox
155- Add-Log " Folder: $folder " $logBox
156- Add-Log " Quality: $quality " $logBox
157- Add-Log " Format: $format " $logBox
158- Add-Log " Delete originals: $deleteOriginals " $logBox
159- Add-Log " Recursive: $recursive " $logBox
156+ Add-Log " Starting conversion to $format ..." $logBox
160157 Add-Log " " $logBox
161158
162159 $buttonStart.Enabled = $false
163160
164161 try {
165- # Gather files based on recursive flag
166162 if ($recursive ) {
167163 $files = Get-ChildItem - Path $folder - File - Recurse | Where-Object {
168- $_.Extension -in ' .jpg' , ' .jpeg' , ' .JPG ' , ' .JPEG ' , ' .png ' , ' .PNG ' , ' .avif ' , ' .AVIF'
164+ $_.Extension -in ' .jpg' , ' .jpeg' , ' .png ' , ' .avif ' , ' .JPG ' , ' .JPEG ' , ' .PNG ' , ' .AVIF'
169165 }
170166 } else {
171167 $files = Get-ChildItem - Path $folder - File | Where-Object {
172- $_.Extension -in ' .jpg' , ' .jpeg' , ' .JPG ' , ' .JPEG ' , ' .png ' , ' .PNG ' , ' .avif ' , ' .AVIF'
168+ $_.Extension -in ' .jpg' , ' .jpeg' , ' .png ' , ' .avif ' , ' .JPG ' , ' .JPEG ' , ' .PNG ' , ' .AVIF'
173169 }
174170 }
175171
@@ -178,21 +174,20 @@ $buttonStart.Add_Click({
178174 $fail = 0
179175
180176 if ($total -eq 0 ) {
181- Add-Log " No PNG/JPG/JPEG/AVIF files found in the selected folder ." $logBox
177+ Add-Log " No PNG/JPG/JPEG/AVIF files found." $logBox
182178 } else {
183179 Add-Log " Found $total file(s)." $logBox
184180 Add-Log " " $logBox
185181
186182 foreach ($file in $files ) {
187- # Determine output file path
183+
188184 $outPath = [System.IO.Path ]::ChangeExtension($file.FullName , " .$format " )
185+
189186 Add-Log " Converting: $ ( $file.FullName ) " $logBox
190187 Add-Log " --> $outPath " $logBox
191188
192- # Perform conversion using ImageMagick
193189 & magick $file.FullName - auto- orient - quality $quality $outPath
194190
195- # Check for success and handle deletion of original
196191 if (Test-Path $outPath ) {
197192 if ($deleteOriginals ) {
198193 Remove-Item $file.FullName - ErrorAction SilentlyContinue
@@ -205,7 +200,6 @@ $buttonStart.Add_Click({
205200 }
206201
207202 Add-Log " " $logBox
208- $form.Refresh ()
209203 }
210204
211205 Add-Log " -----------------------------" $logBox
0 commit comments