-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTo-Webp.ps1
68 lines (54 loc) · 1.84 KB
/
ConvertTo-Webp.ps1
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
# Written by Reece Mercer
<#
.SYNOPSIS
Takes a folder of images and converts them to WebP format
.DESCRIPTION
Downloads libwebp, if required, and compresses images in a given folder
.PARAMETER downloadLibWebp
Switch denoting whether libwebp is needed to be downloaded or not
.PARAMETER compressionFactor
The amount of compression to apply (default 80)
.PARAMETER directory
The path of the folder of images that is to be converted
.INPUTS
None.
.OUTPUTS
None
.EXAMPLE
... you don't have libwebp installed
PS> ConvertTo-Webp -downloadLibWebp -directory ./images
.EXAMPLE
... you do have libwebp installed
PS> ConvertTo-Webp -directory ./images
.EXAMPLE
... override default compression factor
PS> ConvertTo-Webp -directory ./images -compressionFactor 20
#>
param (
[switch]$downloadLibWebp,
[parameter(Mandatory = $false)]
[ValidateRange(1, 100)]
[int]$compressionFactor = 80,
[parameter(Mandatory = $true)]
[string]$directory
)
# What version of cwebp binaries do we want?
$version = "1.1.0"
$download = "https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-$version-windows-x64.zip"
if ($downloadLibWebp) {
Invoke-WebRequest $download -OutFile ./libwebp.zip; if ($?) { Expand-Archive ./libwebp.zip -DestinationPath ./libwebp -Force }
Remove-Item ./libwebp.zip
if (Test-Path $directory) {
Get-ChildItem $directory | ForEach-Object {
& ".\libwebp\libwebp-$version-windows-x64\bin\cwebp.exe" -q $compressionFactor $_.FullName -o "$($_.Name).webp"
}
}
}
else {
if (Test-Path $directory) {
Get-ChildItem $directory | ForEach-Object {
cwebp.exe -q $compressionFactor $_.FullName -o "$($_.Name).webp"
}
}
}
Remove-Item -Recurse ./libwebp -Force