Last active
September 18, 2021 16:56
-
-
Save ninmonkey/72f028b29a1588f038a456bf85d175be to your computer and use it in GitHub Desktop.
Embed images inside Power BI Reports ( DAX with inline base64 images)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| You can convert to base64 using this website: | |
| https://www.browserling.com/tools/image-to-base64 | |
| This runs on 'PowerShell' | |
| ( It requires changes to run on 'Windows Powershell' (legacy) ) | |
| For usage jump to the very last lines in this file: | |
| > $config = $runKwargs.DaxPng | |
| > ConvertImagesTo-Base64 @config | |
| #> | |
| $Paths = @{ | |
| Base = $PSScriptRoot | Get-Item | |
| } | |
| $Paths.ImportImage = Join-Path $Paths.Base 'images' | Get-Item -ea stop | |
| $Paths.ExportBase64 = Join-Path $Paths.Base 'base64' | Get-Item -ea stop | |
| function ConvertImagesTo-Base64 { | |
| <# | |
| .description | |
| converts all matching images as base64 encoding | |
| -ExportAsDax | |
| this outputs source code for the DAX measure | |
| #> | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter( | |
| Mandatory, Position = 0, | |
| HelpMessage = "Base path to search for images")] | |
| [string]$Path, | |
| [Parameter( | |
| Mandatory, Position = 1, | |
| HelpMessage = "Path to export base64 files")] | |
| [string]$ExportPath, | |
| [Parameter( | |
| HelpMessage = "Regex to apply on filenames")] | |
| [string]$ExtensionRegex, | |
| [Parameter( | |
| HelpMessage = "Output as a single DAX string instead of creating files" | |
| )] | |
| [switch]$ExportAsDax, | |
| [Parameter( | |
| HelpMessage = "Copy DAX directly to your clipboard ( otherwise print to the console )" | |
| )] | |
| [switch]$SaveToClipboard | |
| ) | |
| $MimePrefix = @{ | |
| jpg = 'data:image/jpeg;base64,' | |
| jpeg = 'data:image/jpeg;base64,' | |
| png = 'data:image/png;base64,' | |
| } | |
| $template = @{ | |
| DAX = @' | |
| MeasureName = | |
| SWITCH ( | |
| SELECTEDVALUE( Table[Column] ), | |
| {0} | |
| "Default" | |
| ) | |
| '@ | |
| SwitchItem = '"{0}", "{1} | |
| {2}",' | |
| } | |
| $Path = $Path | Get-Item -ErrorAction stop | |
| $ExportPath = $ExportPath | Get-Item -ErrorAction stop | |
| $kwargs_ls = @{ | |
| Path = $Path | |
| Recurse = $false | |
| } | |
| $ImageList = Get-ChildItem @kwargs_ls | |
| | Where-Object { | |
| $FileExtension = $_.Extension -replace '^\.', '' | |
| $FileExtension -match $ExtensionRegex | |
| } | |
| if ($null -eq $ImageList -or $ImageList.count -eq 0) { | |
| $kwargs_ls | Format-Pair -Title 'kwargs' | |
| $PSBoundParameters | Format-Table -Title 'PSBound' | |
| throw "No Files found!" | |
| } | |
| "Found files: $($ImageList.Name -join ', ' )" | Write-Verbose | |
| "Wrote: $($ImageList.Count) files" | Write-Verbose | |
| $ImageList | Join-String -Property 'Name' -OutputPrefix 'Found: ' -sep ', ' | Write-Verbose | |
| if (! $ExportAsDax) { | |
| $ImageList | ForEach-Object { | |
| $curImage = $_ | |
| $content = Get-Content $curImage -AsByteStream | |
| $base64Text = [convert]::ToBase64String( $content ) | |
| $BaseName = $curImage.Name | |
| $NewName = $BaseName, '-base64.txt' -join '' | |
| $FullPath = Join-Path $Paths.ExportBase64 -ChildPath $NewName | |
| Set-Content -Path $FullPath -Value $base64Text | |
| } | |
| } else { | |
| $ItemsString = '' | |
| $ImageList | ForEach-Object { | |
| $curImage = $_ | |
| $content = Get-Content $curImage -AsByteStream | |
| $base64Text = [convert]::ToBase64String( $content ) | |
| $FileExtension = $curImage.Extension -replace '^\.', '' | |
| $MimeString = $MimePrefix.$FileExtension | |
| if ($null -eq $MimePrefix) { | |
| "Unknown MimeType: {0}`nFor File: {1}" -f ( | |
| $FileExtension, | |
| $curImage.FullName | |
| ) | |
| continue | |
| } | |
| $ItemsString += $template.SwitchItem -f ( | |
| $curImage.BaseName, | |
| $MimeString, | |
| $base64Text | |
| ) | |
| $ItemsString += "`n" | |
| } | |
| $FullDaxMeasure = $template.DAX -f ( | |
| $ItemsString | |
| ) | |
| if ($SaveToClipboard) { | |
| $FullDaxMeasure | Set-Clipboard | |
| } else { | |
| $FullDaxMeasure | |
| } | |
| $FullPath = Join-Path $Paths.Base -ChildPath 'DAX Measure.txt' | |
| Set-Content -Path $FullPath -Value $base64Text | |
| } | |
| } | |
| $regex = @{ | |
| Jpg = 'jp.?g' | |
| Png = 'png' | |
| AllImages = 'jp.?g|png|bmp|gif|webm|webp' | |
| } | |
| $runKwargs = @{} | |
| # export: as text files | |
| $runKwargs.JpgAll = @{ | |
| Path = "$PSScriptRoot/images" | |
| ExportPath = "$PSScriptRoot/base64" | |
| Verbose = $true | |
| ExportAsDax = $false | |
| ExtensionRegex = $regex.Jpg | |
| } | |
| # export: as text files | |
| $runKwargs.PngAll = @{ | |
| Path = "$PSScriptRoot/images" | |
| ExportPath = "$PSScriptRoot/base64" | |
| Verbose = $true | |
| ExportAsDax = $false | |
| ExtensionRegex = $regex.Png | |
| # Filter = '*.png' | |
| } | |
| # export: as DAX source code: saving to clipboard | |
| $runKwargs.DaxPng = @{ | |
| Path = "$PSScriptRoot/images" | |
| ExportPath = "$PSScriptRoot/base64" | |
| Verbose = $true | |
| ExtensionRegex = $regex.Png | |
| ExportAsDax = $true | |
| SaveToClipboard = $true | |
| } | |
| # export: as DAX source code: saving to clipboard | |
| $runKwargs.DaxJpg = @{ | |
| Path = "$PSScriptRoot/images" | |
| ExportPath = "$PSScriptRoot/base64" | |
| Verbose = $true | |
| ExtensionRegex = $regex.Jpg | |
| ExportAsDax = $true | |
| SaveToClipboard = $true | |
| } | |
| # This is the main entyr point | |
| $config = $runKwargs.DaxPng | |
| ConvertImagesTo-Base64 @config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment