Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

powershell - Dynamically insert images in HTML file

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

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

1 Reply

0 votes
by (71.8m points)

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"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...