-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-converter-gui.ps1
More file actions
245 lines (212 loc) · 10.5 KB
/
Copy pathimage-converter-gui.ps1
File metadata and controls
245 lines (212 loc) · 10.5 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function Add-Log {
param([string]$Message, [System.Windows.Forms.TextBox]$LogBox)
$LogBox.AppendText($Message + [Environment]::NewLine)
$LogBox.SelectionStart = $LogBox.Text.Length
$LogBox.ScrollToCaret()
}
$form = New-Object System.Windows.Forms.Form
$form.Text = "PNG/JPG/AVIF -> WebP / AVIF Converter"
$form.StartPosition = "CenterScreen"
$form.Size = New-Object System.Drawing.Size(780, 580)
$form.FormBorderStyle = 'FixedDialog'
$form.MaximizeBox = $false
$form.AllowDrop = $true
$form.Font = New-Object System.Drawing.Font("Segoe UI", 9)
$labelTitle = New-Object System.Windows.Forms.Label
$labelTitle.Text = "Image Converter for Web (PNG/JPG/AVIF -> WebP / AVIF)"
$labelTitle.Location = New-Object System.Drawing.Point(15, 10)
$labelTitle.AutoSize = $true
$labelTitle.Font = New-Object System.Drawing.Font("Segoe UI Semibold", 11)
$form.Controls.Add($labelTitle)
$labelSubtitle = New-Object System.Windows.Forms.Label
$labelSubtitle.Text = "Select a folder with images, set quality and output format, then click Start."
$labelSubtitle.Location = New-Object System.Drawing.Point(15, 35)
$labelSubtitle.AutoSize = $true
$labelSubtitle.ForeColor = [System.Drawing.Color]::Gray
$form.Controls.Add($labelSubtitle)
$groupFolder = New-Object System.Windows.Forms.GroupBox
$groupFolder.Text = " Source folder "
$groupFolder.Location = New-Object System.Drawing.Point(15, 65)
$groupFolder.Size = New-Object System.Drawing.Size(740, 80)
$form.Controls.Add($groupFolder)
$labelFolder = New-Object System.Windows.Forms.Label
$labelFolder.Text = "Folder:"
$labelFolder.Location = New-Object System.Drawing.Point(10, 30)
$labelFolder.AutoSize = $true
$groupFolder.Controls.Add($labelFolder)
$textFolder = New-Object System.Windows.Forms.TextBox
$textFolder.Location = New-Object System.Drawing.Point(70, 27)
$textFolder.Size = New-Object System.Drawing.Size(540, 24)
$textFolder.AllowDrop = $true
$groupFolder.Controls.Add($textFolder)
$buttonBrowse = New-Object System.Windows.Forms.Button
$buttonBrowse.Text = "Browse..."
$buttonBrowse.Location = New-Object System.Drawing.Point(620, 25)
$buttonBrowse.Size = New-Object System.Drawing.Size(90, 28)
$groupFolder.Controls.Add($buttonBrowse)
$folderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$buttonBrowse.Add_Click({
if ($folderDialog.ShowDialog() -eq "OK") { $textFolder.Text = $folderDialog.SelectedPath }
})
$textFolder.Add_DragEnter({
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) { $_.Effect = 'Copy' } else { $_.Effect = 'None' }
})
$textFolder.Add_DragDrop({
$paths = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
if ($paths -and $paths.Length -gt 0) {
$first = $paths[0]
if (Test-Path $first -PathType Container) { $textFolder.Text = $first }
else { $textFolder.Text = [System.IO.Path]::GetDirectoryName($first) }
}
})
$groupSettings = New-Object System.Windows.Forms.GroupBox
$groupSettings.Text = " Settings "
$groupSettings.Location = New-Object System.Drawing.Point(15, 155)
$groupSettings.Size = New-Object System.Drawing.Size(740, 110)
$form.Controls.Add($groupSettings)
$labelQuality = New-Object System.Windows.Forms.Label
$labelQuality.Text = "Quality (0-100):"
$labelQuality.Location = New-Object System.Drawing.Point(10, 30)
$labelQuality.AutoSize = $true
$groupSettings.Controls.Add($labelQuality)
$textQuality = New-Object System.Windows.Forms.TextBox
$textQuality.Location = New-Object System.Drawing.Point(120, 27)
$textQuality.Size = New-Object System.Drawing.Size(50, 24)
$textQuality.Text = "85"
$groupSettings.Controls.Add($textQuality)
$labelQualityHint = New-Object System.Windows.Forms.Label
$labelQualityHint.Text = "(85 is a good default for web)"
$labelQualityHint.Location = New-Object System.Drawing.Point(180, 30)
$labelQualityHint.AutoSize = $true
$labelQualityHint.ForeColor = [System.Drawing.Color]::Gray
$groupSettings.Controls.Add($labelQualityHint)
$labelFormat = New-Object System.Windows.Forms.Label
$labelFormat.Text = "Output format:"
$labelFormat.Location = New-Object System.Drawing.Point(10, 65)
$labelFormat.AutoSize = $true
$groupSettings.Controls.Add($labelFormat)
$comboFormat = New-Object System.Windows.Forms.ComboBox
$comboFormat.Location = New-Object System.Drawing.Point(120, 62)
$comboFormat.Size = New-Object System.Drawing.Size(100, 24)
$comboFormat.DropDownStyle = 'DropDownList'
[void]$comboFormat.Items.Add("webp")
[void]$comboFormat.Items.Add("avif")
$comboFormat.SelectedIndex = 0
$groupSettings.Controls.Add($comboFormat)
$groupOptions = New-Object System.Windows.Forms.GroupBox
$groupOptions.Text = " Options "
$groupOptions.Location = New-Object System.Drawing.Point(15, 275)
$groupOptions.Size = New-Object System.Drawing.Size(740, 90)
$form.Controls.Add($groupOptions)
$checkDelete = New-Object System.Windows.Forms.CheckBox
$checkDelete.Text = "Delete original source files after successful conversion"
$checkDelete.Location = New-Object System.Drawing.Point(10, 25)
$checkDelete.AutoSize = $true
$checkDelete.Checked = $true
$groupOptions.Controls.Add($checkDelete)
$checkRecursive = New-Object System.Windows.Forms.CheckBox
$checkRecursive.Text = "Include subfolders (recursive)"
$checkRecursive.Location = New-Object System.Drawing.Point(10, 50)
$checkRecursive.AutoSize = $true
$groupOptions.Controls.Add($checkRecursive)
$buttonStart = New-Object System.Windows.Forms.Button
$buttonStart.Text = "Start conversion"
$buttonStart.Location = New-Object System.Drawing.Point(15, 380)
$buttonStart.Size = New-Object System.Drawing.Size(160, 36)
$buttonStart.Font = New-Object System.Drawing.Font("Segoe UI Semibold", 9)
$form.Controls.Add($buttonStart)
$labelLog = New-Object System.Windows.Forms.Label
$labelLog.Text = "Log output:"
$labelLog.Location = New-Object System.Drawing.Point(15, 425)
$labelLog.AutoSize = $true
$form.Controls.Add($labelLog)
$logBox = New-Object System.Windows.Forms.TextBox
$logBox.Location = New-Object System.Drawing.Point(15, 445)
$logBox.Size = New-Object System.Drawing.Size(740, 90)
$logBox.Multiline = $true
$logBox.ScrollBars = "Vertical"
$logBox.ReadOnly = $true
$logBox.Font = New-Object System.Drawing.Font("Consolas", 9)
$form.Controls.Add($logBox)
$buttonStart.Add_Click({
$folder = $textFolder.Text.Trim()
if ([string]::IsNullOrWhiteSpace($folder) -or -not (Test-Path $folder)) {
[System.Windows.Forms.MessageBox]::Show("Please select a valid folder.","Error","OK","Error")
return
}
$magickCmd = Get-Command magick -ErrorAction SilentlyContinue
if (-not $magickCmd) {
[System.Windows.Forms.MessageBox]::Show("ImageMagick 'magick' command not found.`r`nPlease install ImageMagick.","ImageMagick not found","OK","Error")
return
}
$qualityText = $textQuality.Text.Trim()
[int]$quality = 0
if (-not [int]::TryParse($qualityText, [ref]$quality) -or $quality -lt 0 -or $quality -gt 100) {
[System.Windows.Forms.MessageBox]::Show("Quality must be an integer between 0 and 100.","Error","OK","Error")
return
}
$format = $comboFormat.SelectedItem
$deleteOriginals = $checkDelete.Checked
$recursive = $checkRecursive.Checked
$logBox.Clear()
Add-Log "Starting conversion..." $logBox
Add-Log "Folder: $folder" $logBox
Add-Log "Format: $format" $logBox
Add-Log "Quality: $quality" $logBox
Add-Log "Recursive: $recursive" $logBox
Add-Log "Delete originals: $deleteOriginals" $logBox
Add-Log "" $logBox
$buttonStart.Enabled = $false
try {
$files = Get-ChildItem -Path $folder -File -Recurse:$recursive | Where-Object {
$_.Extension -in '.jpg','.jpeg','.png','.avif','.webp','.JPG','.JPEG','.PNG','.AVIF','.WEBP'
}
$total = $files.Count
$success = 0
$fail = 0
foreach ($file in $files) {
if ($format -eq 'webp') {
# temp + csere, hogy ne legyen dupla .webp és ne írja felül a futó forrást
$outPath = Join-Path $file.DirectoryName ($file.BaseName + '.webp')
$tempPath = Join-Path $file.DirectoryName ($file.BaseName + '.tmp-convert.webp')
Add-Log "Converting: $($file.FullName)" $logBox
& magick "$($file.FullName)" -auto-orient -quality $quality "$tempPath"
if (Test-Path $tempPath) {
Remove-Item $file.FullName -ErrorAction SilentlyContinue
Rename-Item $tempPath $outPath -ErrorAction SilentlyContinue
$success++
Add-Log " OK -> $outPath" $logBox
} else {
$fail++
Add-Log " ERROR" $logBox
}
} else {
$outPath = [System.IO.Path]::ChangeExtension($file.FullName, ".$format")
Add-Log "Converting: $($file.FullName)" $logBox
& magick "$($file.FullName)" -auto-orient -quality $quality "$outPath"
if (Test-Path $outPath) {
if ($deleteOriginals -and ($file.FullName -ne $outPath)) {
Remove-Item $file.FullName -ErrorAction SilentlyContinue
}
$success++
Add-Log " OK -> $outPath" $logBox
} else {
$fail++
Add-Log " ERROR" $logBox
}
}
}
Add-Log "" $logBox
Add-Log "Done." $logBox
Add-Log "Total: $total" $logBox
Add-Log "Success: $success" $logBox
Add-Log "Failed: $fail" $logBox
}
finally {
$buttonStart.Enabled = $true
}
})
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.Application]::Run($form)