Public/New-GhostImage.ps1

function New-GhostImage {
    <#
    .SYNOPSIS
        Uploads an image to the Ghost site.

    .DESCRIPTION
        Uploads an image file and returns the address Ghost stored it at. Use that address wherever a post takes
        an image: in its content, or as its feature image.

        Ghost accepts WEBP, JPEG, GIF, PNG and SVG files, and ICO files for an icon. It keeps the name of the
        file and adds something to it when an image with that name is already there, so every upload gets an
        address of its own. An image cannot be removed again through the API.

        Sync-GhostPost uploads the images that a Markdown file refers to by itself.

    .EXAMPLE
        New-GhostImage -Path ./images/diagram.png

        Uploads the image and returns its address in the property url.

    .EXAMPLE
        New-GhostPost -Title 'With a picture' -FeatureImage (New-GhostImage -Path ./cover.jpg).url

        Uploads an image and creates a draft that has it as its feature image.

    .EXAMPLE
        Get-ChildItem -Path ./images -File | New-GhostImage

        Uploads every file in the folder images.

    .EXAMPLE
        New-GhostImage -Path ./logo.png -Purpose icon

        Uploads a square image that is checked as a site icon.

    .LINK
        https://docs.ghost.org/admin-api/images/uploading-an-image

    .LINK
        https://docs.ghost.org/admin-api/images/overview
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('Ghost.Image')]
    param(
        # The image file to upload. The path is taken literally: wildcards are not expanded.
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('FullName')]
        [ValidateNotNullOrEmpty()]
        [string] $Path,

        # The file name to store the image under, with its extension. Defaults to the name of the file.
        [ValidateNotNullOrEmpty()]
        [string] $Name,

        # What the image is for, which decides how Ghost checks it. A profile image and an icon have to be
        # square, an icon at least 60 by 60 pixels, and only an icon can be an ICO file. Without it Ghost takes
        # image.
        [ValidateSet('image', 'profile_image', 'icon')]
        [string] $Purpose,

        # Text of your own that Ghost returns with the address, in the property ref, such as the original path
        # of the image.
        [string] $Reference
    )

    begin {
        Test-GhostConnection
        $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name

        # Ghost checks the content type of the file as well as its extension.
        $contentTypes = @{
            '.gif'  = 'image/gif'
            '.ico'  = 'image/x-icon'
            '.jpeg' = 'image/jpeg'
            '.jpg'  = 'image/jpeg'
            '.png'  = 'image/png'
            '.svg'  = 'image/svg+xml'
            '.svgz' = 'image/svg+xml'
            '.webp' = 'image/webp'
        }
    }

    process {
        try {
            if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
                throw "The image file '$Path' does not exist."
            }
            $file = (Resolve-Path -LiteralPath $Path).ProviderPath
            $content = [System.IO.File]::ReadAllBytes($file)
            if ($content.Length -eq 0) {
                throw "The image file '$Path' is empty."
            }

            $fileName = [System.IO.Path]::GetFileName($file)
            if ($PSBoundParameters.ContainsKey('Name')) {
                $fileName = $Name
            }
            # Any other extension is sent as it is, and Ghost decides whether it takes the file.
            $contentType = $contentTypes[[System.IO.Path]::GetExtension($fileName).ToLowerInvariant()]
            if (-not $contentType) {
                $contentType = 'application/octet-stream'
            }

            # An upload is a form instead of JSON. Its text fields are listed under Body, like those of any body.
            $fields = @{}
            foreach ($parameter in $resources.Body.Keys) {
                if ($PSBoundParameters.ContainsKey($parameter)) {
                    $value = ConvertTo-ApiValue -Value $PSBoundParameters[$parameter] -AsString
                    $fields[$resources.Body[$parameter]] = $value
                }
            }
            $form = @{
                Fields          = $fields
                FileName        = $fileName
                FileContent     = $content
                FileContentType = $contentType
            }
            $form = ConvertTo-MultipartContent @form

            $uri = New-URIString -Endpoint $resources.URI

            if ($PSCmdlet.ShouldProcess($Path, $resources.Description)) {
                $request = @{
                    Uri         = $uri
                    Method      = $resources.Method
                    Content     = $form.Content
                    ContentType = $form.ContentType
                    Success     = $resources.Success
                }
                $result = Submit-Request @request
                $result = Test-ReturnFormat -Result $result -Location $resources.Result

                Set-ObjectTypeName -InputObject $result -TypeName $resources.TypeName
            }
        }
        catch {
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}