Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Embedding a png image into an html file generated from a Powershell script

I would like to embed a png image into an html file which will then be sent as an attachment to an email.

What code would I use to embed an image such an png or jpg image into an html file?
Does the image have to be pulled externally over the internet or can it be embedded into the html as it were?

A powershell script is being used to create the html file. 
Avatar of Katy Nicholson MVP
Katy Nicholson MVP
Flag of United Kingdom of Great Britain and Northern Ireland image

You can do either - link to a file hosted on the Internet, or embedded into the HTML.

To embed you'd use a tool to convert the image into Base64 e.g. Base64 Image Encoder | Online Tool (base64-encode.org)  and in your HTML you'd use something like:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAaSURBVDhPY3gro0ISGtVADBrVQAwadBpkVADPEy0Q0V9lSAAAAABJRU5ErkJggg==">

Open in new window


If you've just got a static image you want to use then that's probably the easiest way, otherwise if you've got different images depending on various things and want to do the base64 encoding in PowerShell you'd need something like:

[convert]::ToBase64String((Get-Content -path "your_file_path" -Encoding byte))

Open in new window


which will output the base64 string, to insert into the code above. You'd also need to set the content type in the string accordingly.
Avatar of E=mc2

ASKER

Thanks Katy, much appreciated.
With regards to the Base 64 encoder, is the image stored on their online database, or is it stored within the algorithm numbers?

ASKER CERTIFIED SOLUTION
Avatar of Katy Nicholson MVP
Katy Nicholson MVP
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

Thanks Katy.
So the image is not saved in any way on anyone else's server?
Generally no (as they delete it after converting) but if you need to guarantee this then use the PowerShell code to generate the base64 data rather than uploading to the web converter.
Avatar of E=mc2

ASKER

Ok let me consider that then and test it.. many thanks.