-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert-ImageToBase64.ps1
More file actions
41 lines (38 loc) · 1.67 KB
/
Convert-ImageToBase64.ps1
File metadata and controls
41 lines (38 loc) · 1.67 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
function Convert-ImageToBase64 {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidateFileExists -Path $_ })]
[string]$path = $(Throw ((Get-ResStr 'PARAM_MANDATORY_MISSED') -f 'path', $myInvocation.Mycommand))
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
New-Variable -Name 'base64' -Scope 'Private' -Value ('')
New-Variable -Name 'content' -Scope 'Private' -Value ($null)
New-Variable -Name 'extension' -Scope 'Private' -Value ('')
New-Variable -Name 'mimeType' -Scope 'Private' -Value ('')
New-Variable -Name 'result' -Scope 'Private' -Value ('')
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
$content = [System.IO.File]::ReadAllBytes($path)
$base64 = [System.Convert]::ToBase64String($content)
$extension = [System.IO.Path]::GetExtension($path).ToLower()
switch ($extension) {
".jpg" { $mimeType = "image/jpeg" }
".jpeg" { $mimeType = "image/jpeg" }
".gif" { $mimeType = "image/gif" }
".png" { $mimeType = "image/png" }
".bmp" { $mimeType = "image/bmp" }
".ico" { $mimeType = "image/x-icon" }
".pdf" { $mimeType = "application/pdf" }
default { throw "Unknown image file type: $extension" }
}
$result = "data:$mimeType;base64,$base64"
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
return $result
}
# Test: Convert-ImageToBase64 -path 'C:\temp\Eulanda.jpg'
}