trevor.psm1
function whoareyou { 'Trevor Sullivan' } function New-SocialImage { <# .SYNOPSIS Creates a social media image with a text overlay, using .NET Core drawing capabilities. .PARAMETER OutputPath Specify the relative or fully qualified path on the filesystem where you'd like to create the output image. .PARAMETER Text The text you'd like to overlay on the social media image. .EXAMPLE # This example will create a new image, with some default text, as output.png in the "current" directory. New-SocialImage .EXAMPLE # This example will output to your home folder. New-SocialImage -OutputPath $HOME/output234.png #> [CmdletBinding()] param ( [Parameter(Mandatory = $false)] [string] $Text = 'Sample text for a social media post. Please specify the -text parameter.', [Parameter(Mandatory = $false)] [string] $OutputPath = 'output.png' ) try { # Create a new empty bitmap $Bitmap = [System.Drawing.Bitmap]::new(1024, 1024) # Get the Graphics drawing object from the image $Graphics = [System.Drawing.Graphics]::FromImage($Bitmap) # Create a gradient background for the image $Mode = [System.Drawing.Drawing2D.LinearGradientMode]::Vertical $Rectangle = [System.Drawing.Rectangle]::new(0,0,1024,1024) $Brush = [System.Drawing.Drawing2D.LinearGradientBrush]::new($Rectangle, [System.Drawing.Color]::RebeccaPurple, [System.Drawing.Color]::Cyan, $Mode) $Brush.SetSigmaBellShape(0.5) $Graphics.FillRectangle($Brush, $Rectangle) # Specify some text drawing options $Graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias $Graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $Graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality # Draw text on top of the gradient $Brush = [System.Drawing.SolidBrush]::new([System.Drawing.Color]::White) $Font = [System.Drawing.Font]::new([System.Drawing.FontFamily]::GenericSansSerif, 60, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Pixel) $RectangleF = [System.Drawing.RectangleF]::new(300,300,500,500) $StringFormat = [System.Drawing.StringFormat]::new() $StringFormat.Alignment = [System.Drawing.StringAlignment]::Center $StringFormat.LineAlignment = [System.Drawing.StringAlignment]::Center $Graphics.DrawString($Text, $Font, $Brush, $RectangleF) $Graphics.Flush() # Save the output file as a PNG $Bitmap.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Png) } catch { } finally { } } |