Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try dynamically insert images in my HTML file, when I say dynamically it mean when it will be asked by a user, here 2 approce that I imagine, for both case user must before running the code put the images in a specific folder.

Via a switch parameter like: generate-html -includeImages, if switch param is true then convert all images in MyImagefolder and insert one after one in my output.

or

Via a parameter that accept multiple value and in this case user must enter the images names like generate-html -includeImages image1.gif, image2.gif

Here the code that I use for insert one image to my HTML file:

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$image = "$PSScriptRootimg.gif"
$ImageBits = [Convert]::ToBase64String((Get-Content $image -Encoding Byte))
$ImageHTML = "<img src=data:image/png;base64,$($ImageBits) alt='My Image'/>"

Then I insert my variable in my convertto-html command.

ConvertTo-Html -Body $style -PreContent "$imageHTML" | Out-File "$PSScriptRoot
eport.html"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
312 views
Welcome To Ask or Share your Answers For Others

1 Answer

An array of images could be handled like this:

$images = Get-ChildItem "C:path	o*.gif"
$ImageHTML = $images | % {
  $ImageBits = [Convert]::ToBase64String((Get-Content $_ -Encoding Byte))
  "<img src=data:image/png;base64,$($ImageBits) alt='My Image'/>"
}

Whether you want to use a switch -IncludeImage and read the images from a pre-defined folder or pass the image paths via the -IncludeImage parameter is up to you. The latter is probably the more versatile approach, as it makes your code independent from a particular folder, but both approaches will work.

A function using a switch parameter might look like this:

function Generate-Html {
  Param(
    [Switch]
    [bool]$IncludeImages
  )

  if ($IncludeImages) {
    $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
    $images = Get-ChildItem "$PSScriptRoot*.gif"
    $ImageHTML = $images | % {
      $ImageBits = [Convert]::ToBase64String((Get-Content $_ -Encoding Byte))
      "<img src=data:image/png;base64,$($ImageBits) alt='My Image'/>"
    }

    ConvertTo-Html -Body $style -PreContent $imageHTML |
      Out-File "$PSScriptRoot
eport.html"
  }
}

while a function using an array parameter might look like this:

function Generate-Html {
  Param(
    [Parameter()]
    [string[]]$IncludeImages
  )

  if ($IncludeImages) {
    $ImageHTML = $IncludeImages | % {
      $ImageBits = [Convert]::ToBase64String((Get-Content $_ -Encoding Byte))
      "<img src=data:image/png;base64,$($ImageBits) alt='My Image'/>"
    }

    ConvertTo-Html -Body $style -PreContent $imageHTML |
      Out-File "C:path	o
eport.html"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...