Skip to content

Instantly share code, notes, and snippets.

@pferreirafabricio
Last active March 30, 2024 16:03
Show Gist options
  • Save pferreirafabricio/6120ee40c2455477411454be7d1359f4 to your computer and use it in GitHub Desktop.
Save pferreirafabricio/6120ee40c2455477411454be7d1359f4 to your computer and use it in GitHub Desktop.
Script to loop through a folder with images and convert all of them to a Base64 string
<#
Usage on Linux:
pwsh ./convertImagesToBase64.ps1 /home/user/folder/images
Usage on Windows:
./convertImagesToBase64.ps1 C:/Path/To/Imagens
#>
$folderPath = $args[0]
Write-Output "Starting the conversion"
$allImages = Get-ChildItem $folderPath -Filter *.png
foreach ($image in $allImages) {
$fileName = $image.Name
Write-Output "Converting the image: $fileName"
# OBS: For more information on how convert a file to base64 take a look at this gist: [png-to-base64.ps1](https://gist.github.com/pferreirafabricio/97c1c68057e406ff0f98840659973f0d)
[String]$base64 = [convert]::ToBase64String((Get-Content $image.FullName -Raw -AsByteStream))
Write-Output $base64
}
Write-Output "All images were converted"
@pferreirafabricio
Copy link
Author

OBS: Remember to download PowerShell on Linux: Installing PowerShell on Linux

@pferreirafabricio
Copy link
Author

For more information on how convert a file to base64 take a look at this gist: png-to-base64.ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment