PSMemeGenerator.psm1
|
#Region './Private/Invoke-MemeImageModification.ps1' -1 function Invoke-MemeImageModification { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('InjectionRisk.AddType', '', Justification = 'Suppress false positives in PSRule for Add-Type usage')] [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$ImagePath, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$OutputPath, [Parameter()] [string]$TopText = '', [Parameter()] [string]$BottomText = '' ) begin { Write-Verbose "Starting $($MyInvocation.MyCommand)" if (-not $IsWindows) { throw 'This function requires Windows OS due to System.Drawing dependencies.' } if (-not ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'System.Drawing' })) { Add-Type -AssemblyName 'System.Drawing' } } process { try { Write-Verbose "Modifying image $ImagePath and saving to $OutputPath" $bitmap = [System.Drawing.Image]::FromFile($ImagePath) $graphics = [System.Drawing.Graphics]::FromImage($bitmap) $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAlias $brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White) $font = New-Object System.Drawing.Font('Impact', 40, [System.Drawing.FontStyle]::Bold) $padding = 10 if (-not [string]::IsNullOrWhiteSpace($TopText)) { $text = $TopText.ToUpper() $size = $graphics.MeasureString($text, $font) $x = [float][Math]::Max(($bitmap.Width - $size.Width) / 2, $padding) $point = New-Object System.Drawing.PointF($x, [float]$padding) $graphics.DrawString($text, $font, $brush, $point) } if (-not [string]::IsNullOrWhiteSpace($BottomText)) { $text = $BottomText.ToUpper() $size = $graphics.MeasureString($text, $font) $x = [float][Math]::Max(($bitmap.Width - $size.Width) / 2, $padding) $y = [float]($bitmap.Height - $size.Height - $padding) $point = New-Object System.Drawing.PointF($x, $y) $graphics.DrawString($text, $font, $brush, $point) } $jpegCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' } $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, 100L) [void] ($bitmap.Save($OutputPath, $jpegCodec, $encoderParams)) } catch { Write-Verbose "$($MyInvocation.MyCommand) Operation failed: $_" Write-Verbose "StackTrace: $($_.ScriptStackTrace)" throw $_ } finally { if ($null -ne $graphics) { $graphics.Dispose() } if ($null -ne $bitmap) { $bitmap.Dispose() } if ($null -ne $brush) { $brush.Dispose() } if ($null -ne $font) { $font.Dispose() } if ($null -ne $encoderParams) { $encoderParams.Dispose() } } } end { Write-Verbose "Completed $($MyInvocation.MyCommand)" } } #EndRegion './Private/Invoke-MemeImageModification.ps1' 82 #Region './Public/Get-MemeTemplate.ps1' -1 function Get-MemeTemplate { <# .SYNOPSIS Gets a list of popular meme templates from Imgflip. .DESCRIPTION Retrieves the top 100 most popular meme templates from the Imgflip API. Returns objects containing the meme ID, name, URL, width, and height. .EXAMPLE Get-MemeTemplate Returns all available meme templates. .EXAMPLE Get-MemeTemplate -Name 'Drake' Finds meme templates with 'Drake' in the name. .OUTPUTS PSCustomObject #> [CmdletBinding()] param ( [Parameter(Position = 0)] [string] $Name ) begin { Write-Verbose "Starting $($MyInvocation.MyCommand)" } process { try { $uri = 'https://api.imgflip.com/get_memes' Write-Verbose "Fetching meme templates from $uri" $response = Invoke-RestMethod -Uri $uri -Method Get if ($response.success) { foreach ($meme in $response.data.memes) { if ([string]::IsNullOrEmpty($Name) -or $meme.name -match $Name) { [PSCustomObject]@{ Id = $meme.id Name = $meme.name Url = $meme.url Width = $meme.width Height = $meme.height BoxCount = $meme.box_count } } } } else { throw "Imgflip API returned an error: $($response.error_message)" } } catch { Write-Verbose "$($MyInvocation.MyCommand) Operation failed: $_" Write-Verbose "StackTrace: $($_.ScriptStackTrace)" throw $_ } } end { Write-Verbose "Completed $($MyInvocation.MyCommand)" } } #EndRegion './Public/Get-MemeTemplate.ps1' 66 #Region './Public/New-Meme.ps1' -1 function New-Meme { <# .SYNOPSIS Creates a new meme by applying text to an image template. .DESCRIPTION Downloads a meme template from a URL and applies top and bottom text using System.Drawing. Requires Windows OS due to System.Drawing dependencies in modern .NET. .PARAMETER Id The ID of the source meme template from Imgflip. .PARAMETER Name The name of the source meme template from Imgflip. .PARAMETER Url The URL of the source meme image. .PARAMETER TopText The text to display at the top of the meme. .PARAMETER BottomText The text to display at the bottom of the meme. .PARAMETER OutputPath The path where the generated meme image will be saved. .EXAMPLE New-Meme -Name "Drake" -TopText "WHEN YOU WRITE" -BottomText "A POWERSHELL MODULE" -OutputPath ".\meme.jpg" Creates a meme using the first template matching "Drake" and saves it to meme.jpg. .EXAMPLE New-Meme -Id "181913649" -TopText "WHEN YOU WRITE" -BottomText "A POWERSHELL MODULE" -OutputPath ".\meme.jpg" Creates a meme using the template with ID "181913649" and saves it to meme.jpg. .OUTPUTS System.IO.FileInfo #> [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ByName')] param ( [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [string] $Id, [Parameter(Mandatory, ParameterSetName = 'ByName', ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [string] $Name, [Parameter(Mandatory, ParameterSetName = 'ByUrl', ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [string] $Url, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $OutputPath, [Parameter(Mandatory = $false)] [string] $TopText = '', [Parameter(Mandatory = $false)] [string] $BottomText = '' ) begin { Write-Verbose "Starting $($MyInvocation.MyCommand)" } process { try { $resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputPath) $targetUrl = $Url if ($PSCmdlet.ParameterSetName -eq 'ById') { Write-Verbose "Looking up meme template by ID: $Id" $template = Get-MemeTemplate | Where-Object Id -EQ $Id | Select-Object -First 1 if (-not $template) { throw "Could not find a meme template with ID '$Id'." } $targetUrl = $template.Url } elseif ($PSCmdlet.ParameterSetName -eq 'ByName') { Write-Verbose "Looking up meme template by Name: $Name" $template = Get-MemeTemplate -Name $Name | Select-Object -First 1 if (-not $template) { throw "Could not find a meme template matching Name '$Name'." } $targetUrl = $template.Url } if ($PSCmdlet.ShouldProcess($resolvedPath, "Create meme from $targetUrl")) { Write-Verbose "Downloading image from $targetUrl" $tempFile = [System.IO.Path]::GetTempFileName() try { Invoke-WebRequest -Uri $targetUrl -OutFile $tempFile Invoke-MemeImageModification -ImagePath $tempFile -OutputPath $resolvedPath -TopText $TopText -BottomText $BottomText Get-Item -Path $resolvedPath } finally { if (Test-Path $tempFile) { Remove-Item -Path $tempFile -Force } } } } catch { Write-Verbose "$($MyInvocation.MyCommand) Operation failed: $_" Write-Verbose "StackTrace: $($_.ScriptStackTrace)" throw $_ } } end { Write-Verbose "Completed $($MyInvocation.MyCommand)" } } #EndRegion './Public/New-Meme.ps1' 120 |