Skip to content

Instantly share code, notes, and snippets.

@colinMac
Created May 10, 2017 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save colinMac/37e823f39eb5d68961defb5f0109f647 to your computer and use it in GitHub Desktop.
Save colinMac/37e823f39eb5d68961defb5f0109f647 to your computer and use it in GitHub Desktop.
Powershell script to convert an image to base64 data uri format and store in a text file
Param([String]$path)
[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,"
$elements[$max] = "txt"
[String]$txtPath = $elements -join "."
if(!(Test-Path $path)) {
echo ""
echo "Unable to find image file: $path"
break
}
if((Test-Path $txtPath)) {
echo ""
echo "Output file already exists: $txtPath, clearing file"
Clear-Content $txtPath
}
} else {
break
}
[String]$base64 = [convert]::ToBase64String((Get-Content $path -encoding byte))
Write-Output ($uri + $base64) >> $txtPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment