-
Notifications
You must be signed in to change notification settings - Fork 3
/
Imageto64.ps1
48 lines (48 loc) · 1.38 KB
/
Imageto64.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
<#
.SYNOPSIS
ImageTo64.ps1
Created By: Dana Meli
Created Date: August, 2018
Last Modified Date: August 12, 2018
.DESCRIPTION
You feed this an image file and it returns to you Base64 ready to use in a script.
.EXAMPLE
ImageTo64 -Path [<FullPathToImageFile>] -OutFile [<OptionalFullPathForTextFileOut>]
.NOTES
Still under development.
#>
Param([String]$path, [String]$OutFile)
[bool]$valid = ($path -ne "")
if ($valid) {
[String[]]$elements = $path.split(".")
[int]$max = $elements.Count - 1
[String]$ext = $elements[$max]
[String]$uri = "data:image/$($ext);base64,"
if ($OutFile -eq "") {
$elements[$max] = "txt"
[String]$txtPath = $elements -join "."
}
else { [String]$txtPath = $OutFile}
if (!(Test-Path $path)) {
Say ""
Say "Unable to find image file: $path"
break
}
$FileVersion = "Version: 0.1.0"
$host.ui.RawUI.WindowTitle = "ImageTo64 $FileVersion"
if ((Test-Path $txtPath)) {
Say ""
Say "Output file already exists: $txtPath, clearing file"
Clear-Content $txtPath
}
else {
Say ""
Say "Creating output file: $txtPath"
}
}
else { break }
Say "Converting $path"
[String]$base64 = [convert]::ToBase64String((Get-Content $path -AsByteStream))
Say "Writing $txtPath"
Write-Output ($uri + $base64) >> $txtPath
Say "Done"