Skip to content

Commit

Permalink
Merge pull request #15 from rebelinux/dev
Browse files Browse the repository at this point in the history
v0.1.8 public release
  • Loading branch information
rebelinux committed Mar 17, 2024
2 parents 5d82d0b + e333e3a commit 1cfabae
Show file tree
Hide file tree
Showing 29 changed files with 1,320 additions and 297 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Bug Report
description: File a bug report
title: "[BUG]"
labels: ["bug"]
assignees: rebelinux
body:
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/change_request.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Change Request
description: Request a new change or an improvement
title: "[Feature Request]"
labels: ["change request"]
assignees: rebelinux
body:
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.8] - 2024-03-16

### Added

- Added Add-WatermarkToImage cmdlet to add WaterMark text to resulting diagrams.
- Added per format export function:
- ConverTo-Base64
- ConverTo-Png
- ConverTo-Pdf
- ConverTo-Dot
- Added function to allow image rotation to 90 degree (ConvertTo-RotateImage)
- Added Initial support for centralized function to create diagram

### Changed

- Renamed Out-Diagram to Export-Diagrammer
- Improved Get-DiaHTMLNodeTable

### Fixed

- [#12](https://github.com/rebelinux/Diagrammer.Core/issues/12)
- [#13](https://github.com/rebelinux/Diagrammer.Core/issues/13)

## [0.1.7] - 2024-02-26

### Added
Expand Down
4 changes: 2 additions & 2 deletions Diagrammer.Core.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'Diagrammer.Core.psm1'

# Version number of this module.
ModuleVersion = '0.1.7'
ModuleVersion = '0.1.8'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -69,7 +69,7 @@ RequiredModules = @(@{ModuleName = 'PSGraph'; ModuleVersion = '2.1.38.27'; })
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Convert-DiaTableToHTML', 'Get-DiaHtmlLabel', 'Get-DiaHtmlNodeTable', 'Get-DiaHtmlTable', 'Get-NodeIP', 'Get-DiaNodeIcon', 'Out-Diagram', 'Remove-SpecialChar', 'Split-array', 'Test-Image', 'Test-Logo', 'Write-ColorOutput', 'Get-DiaImagePercent'
FunctionsToExport = 'Convert-DiaTableToHTML', 'Get-DiaHtmlLabel', 'Get-DiaHtmlNodeTable', 'Get-DiaHtmlTable', 'Get-NodeIP', 'Get-DiaNodeIcon', 'Export-Diagrammer', 'Remove-SpecialChar', 'Split-array', 'Test-Image', 'Test-Logo', 'Write-ColorOutput', 'Get-DiaImagePercent', 'New-Diagrammer'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
# CmdletsToExport = '*'
Expand Down
145 changes: 145 additions & 0 deletions Src/Private/Add-WatermarkToImage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
function Add-WatermarkToImage {
<#
.SYNOPSIS
Used to add a watermark text to Image
.DESCRIPTION
Takes a string and add it as an 45 degree watermakr to the provided Image file.
.Example
Add-WatermarkToImage ImageInput "c:\Image.png" DestinationPath "c:\Image_Edited.png" -WaterMarkText "Zen PR Solutions" -FontName 'Arial' -FontSize 20 -FontColor 'Red'
.NOTES
Version: 0.1.8
Author: Jonathan Colon
Twitter: @jcolonfzenpr
Github: rebelinux
.PARAMETER ImageInputFile
The Image file Path (PNG, TIFF, JPEG, JPG)
.PARAMETER Base64Input
The image in base64 format
.PARAMETER ImageOutputFile
The path of the resulting edited image
.PARAMETER WaterMarkText
The text to be inserted to the image as a watermark
.PARAMETER FontName
The font name
.PARAMETER FontSize
The font size
.PARAMETER FontColor
The font color [System.Drawing.Color] type (Red, Blue, Yellow etc..)
.PARAMETER FontOpacity
The font color opacity level
#>

param(
[Parameter(
Mandatory = $true,
HelpMessage = 'Please provide the path to the image file path'
)]
[ValidateScript( {
if (Test-Path -Path $_) {
$true
} else {
throw "File $_ not found!"
}
})]
[string] $ImageInput,

[Parameter(
Mandatory = $true,
HelpMessage = 'Please provide the complete filepath to export the diagram'
)]
[string] $DestinationPath,

[Parameter(
Mandatory = $true,
HelpMessage = 'Please provide the text to transform'
)]
[ValidateNotNullOrEmpty()]
[string] $WaterMarkText,

[string] $FontName = 'Arial',

[int] $FontSize = 180,

[System.Drawing.Color] $FontColor = 'Red',

[int] $FontOpacity = 20
)

begin {
# Initialize .net assemblies
Add-Type -AssemblyName System.Windows.Forms
}

process {

$ImageName = Get-ChildItem -Path $ImageInput
# Teporary Image file name
$FileName = $ImageName.BaseName + "_WaterMark" + $ImageName.Extension

# Get the image from the ImageInput path
$Bitmap = [System.Drawing.Image]::FromFile($ImageName.FullName)

# Teporary Image file path
$TempImageOutput = Join-Path -Path ([system.io.path]::GetTempPath()) -ChildPath $FileName

# Initialize the font properties and brush
$FontType = [System.Drawing.Font]::new($FontName, $FontSize, [System.Drawing.FontStyle]::Italic, [System.Drawing.GraphicsUnit]::Pixel, [System.Drawing.GraphicsUnit]::Bold)
$FontColor = [System.Drawing.Color]::FromArgb($FontOpacity, $FontColor)
$SolidBrush = [System.Drawing.SolidBrush]::new($FontColor)

If ($Bitmap, $FontType, $FontColor, $SolidBrush) {

# Get the center of the image
$Grid = [System.Drawing.Point]::new($Bitmap.Width / 2, $Bitmap.Height / 2)
$Graphics = [System.Drawing.Graphics]::FromImage($Bitmap)

# Set the properties to allow the text to be centered
$StringFormat = [System.Drawing.StringFormat]::new()
$StringFormat.Alignment = [System.Drawing.StringAlignment]::Center
$StringFormat.LineAlignment = [System.Drawing.StringAlignment]::Center
$StringFormat.FormatFlags = [System.Drawing.StringFormatFlags]::MeasureTrailingSpaces

# Get the center of the image used to rotate the text in a -45 angle
$Graphics.TranslateTransform($Bitmap.Width / 2, $Bitmap.Height / 2)
$Graphics.RotateTransform(-45)

# Apply the properties to the Bitmap
$Graphics.DrawString($WaterMarkText, $FontType, $SolidBrush, - ($Grid.Width / 2), - ($Grid.Height / 2), $StringFormat)
# Destroy the graphics object
$Graphics.Dispose()

} else {
Write-Information "Unable to add watermark to image!"
}
}

end {
try {
# Save the Image to path define in $TempImageOutput
$Bitmap.Save($TempImageOutput)
# Destroy the Bitmap object
$Bitmap.Dispose()

if ($TempImageOutput) {
Write-Verbose "Successfully added watermark text to $ImageInput image."
if ($PSBoundParameters.ContainsKey('DestinationPath')) {
try {
Copy-Item -Path $TempImageOutput -Destination $DestinationPath
Write-Verbose "Successfully replaced $DestinationPath with $TempImageOutput rotated image."
} catch {
Write-Verbose "Unable to replace $DestinationPath rotated image to $TempImageOutput diagram."
Write-Verbose $($_.Exception.Message)
}
} else {
Write-Verbose "Successfully rotated $ImageInput diagram."
Get-ChildItem -Path $TempImageOutput
}
}

} catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
}
File renamed without changes.
53 changes: 53 additions & 0 deletions Src/Private/ConvertTo-Base64.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function ConvertTo-Base64 {
<#
.SYNOPSIS
Function to export diagram to base64 format.
.DESCRIPTION
Export a diagram in PDF/PNG/SVG formats using PSgraph.
.NOTES
Version: 0.1.8
Author: Jonathan Colon
Twitter: @jcolonfzenpr
Github: rebelinux
.LINK
https://github.com/rebelinux/Diagrammer.Core
#>
[CmdletBinding()]
[OutputType([String])]
Param
(
[Parameter(
Position = 0,
Mandatory = $true,
HelpMessage = 'Please provide image file path'
)]
[ValidateScript( {
if (Test-Path -Path $_) {
$true
} else {
throw "File $_ not found!"
}
})]
[string] $ImageInput
)
process {
Write-Verbose "Trying to convert Graphviz object to Base64 format."
# Code used to output image to base64 format
try {
$Base64 = [convert]::ToBase64String((Get-Content $ImageInput -Encoding byte))
} catch {
Write-Verbose "Unable to convert Graphviz object to Base64 format."
Write-Verbose $($_.Exception.Message)
}
if ($Base64) {
Write-Verbose -Message "Deleting Temporary PNG file: $($ImageInput)"
Remove-Item -Path $ImageInput
Write-Verbose "Successfully converted Graphviz object to Base64 format."
$Base64
} else {
Write-Verbose -Message "Deleting Temporary PNG file: $($ImageInput)"
Remove-Item -Path $ImageInput
}
}
end {}
}
52 changes: 52 additions & 0 deletions Src/Private/ConvertTo-Dot.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function ConvertTo-Dot {
<#
.SYNOPSIS
Function to export diagram to dot format.
.DESCRIPTION
Export a diagram in PDF/PNG/SVG formats using PSgraph.
.NOTES
Version: 0.1.8
Author: Jonathan Colon
Twitter: @jcolonfzenpr
Github: rebelinux
.LINK
https://github.com/rebelinux/Diagrammer.Core
#>
[CmdletBinding()]
[OutputType([String])]
Param
(
[Parameter(
Position = 0,
Mandatory = $true,
HelpMessage = 'Please provide the graphviz dot object'
)]
$GraphObj,
[Parameter(
Position = 1,
Mandatory = $true,
HelpMessage = 'Please provide the file path to export the diagram'
)]
[string] $DestinationPath
)
process {
if ($WaterMarkText) {
Write-ColorOutput -Color 'Red' -String "WaterMark option is not supported with the dot format."
}

try {
Write-Verbose "Trying to convert Graphviz object to DOT format. Destination Path: $DestinationPath."
$Document = Export-PSGraph -Source $GraphObj -DestinationPath $DestinationPath -OutputFormat 'dot' -GraphVizPath $GraphvizPath
} catch {
Write-Verbose "Unable to convert Graphviz object to DOT format."
Write-Verbose $($_.Exception.Message)
}
if ($Document) {
if ($Document) {
Write-Verbose "Successfully converted Graphviz object to DOT format. Saved Path: $DestinationPath."
Get-ChildItem -Path $Document
}
}
}
end {}
}
48 changes: 48 additions & 0 deletions Src/Private/ConvertTo-Pdf.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function ConvertTo-Pdf {
<#
.SYNOPSIS
Function to export diagram to pdf format.
.DESCRIPTION
Export a diagram in PDF/PNG/SVG formats using PSgraph.
.NOTES
Version: 0.1.8
Author: Jonathan Colon
Twitter: @jcolonfzenpr
Github: rebelinux
.LINK
https://github.com/rebelinux/Diagrammer.Core
#>
[CmdletBinding()]
[OutputType([String])]
Param
(
[Parameter(
Position = 0,
Mandatory = $true,
HelpMessage = 'Please provide the graphviz dot object'
)]
$GraphObj,
[Parameter(
Position = 1,
Mandatory = $true,
HelpMessage = 'Please provide the file path to export the diagram'
)]
[string] $DestinationPath
)
process {
try {
Write-Verbose "Trying to convert Graphviz object to PDF format. Destination Path: $DestinationPath."
$Document = Export-PSGraph -Source $GraphObj -DestinationPath $DestinationPath -OutputFormat 'pdf' -GraphVizPath $GraphvizPath
} catch {
Write-Verbose "Unable to convert Graphviz object to PNG format."
Write-Verbose $($_.Exception.Message)
}
if ($Document) {
if ($Document) {
Write-Verbose "Successfully converted Graphviz object to PDF format. Saved Path: $DestinationPath."
Get-ChildItem -Path $Document
}
}
}
end {}
}
Loading

0 comments on commit 1cfabae

Please sign in to comment.