Convert-ImageForTeams.ps1


Description

Purpose

Converts images in a source folder to PNG format and creates thumbnail images.

Detailed Description

This function converts images in a source folder to PNG format and creates thumbnail images. The converted images and thumbnails are saved in a destination folder with GUID-based names.

Back to Top

Usage

Example 1

Convert-ImageForTeams -sourceFolder "C:\Path\To\Source\Images" -destinationFolder "C:\Path\To\Destination"

Back to Top

Notes

No additional notes.

Back to Top


Script

function Convert-ImageForTeams {
    <#
    .SYNOPSIS
    Converts images in a source folder to PNG format and creates thumbnail images.
    
    .DESCRIPTION
    This function converts images in a source folder to PNG format and creates thumbnail images. The converted images and thumbnails are saved in a destination folder with GUID-based names.
    
    .PARAMETER sourceFolder
    The path to the folder containing the source images.
    
    .PARAMETER destinationFolder
    The path to the folder where the converted images and thumbnails will be saved.
    
    .EXAMPLE
    Convert-ImageForTeams -sourceFolder "C:\Path\To\Source\Images" -destinationFolder "C:\Path\To\Destination"
    #>
    param (
        [string]$sourceFolder,
        [string]$destinationFolder
    )

    # Install the necessary .NET namespace
    Add-Type -AssemblyName System.Drawing

    # Dummy function to satisfy the GetThumbnailImage method
    function dummyCallback { return $false }

    # Loop through each image file in the source folder
    Get-ChildItem -Path $sourceFolder -File | ForEach-Object {

        # Generate a GUID for the new image name
        $guid = [guid]::NewGuid().ToString()

        # Create a .NET Bitmap object from the image file
        $originalImage = [System.Drawing.Image]::FromFile($_.FullName)

        # Save the image as a PNG with a GUID-based name
        $originalImage.Save("$destinationFolder\$guid.png", [System.Drawing.Imaging.ImageFormat]::Png)

        # Create a thumbnail image
        $thumbWidth = 278
        $thumbHeight = 159
        $thumbnailImage = $originalImage.GetThumbnailImage($thumbWidth, $thumbHeight, [System.Drawing.Image+GetThumbnailImageAbort]$dummyCallback, [System.IntPtr]::Zero)

        # Save the thumbnail image as a PNG with a GUID-based name and "_thumb" suffix
        $thumbnailImage.Save("$destinationFolder\$guid`_thumb.png", [System.Drawing.Imaging.ImageFormat]::Png)

        # Dispose of the image objects to free resources
        $originalImage.Dispose()
        $thumbnailImage.Dispose()
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top