Script Repository


Optimize User Photo

October 04, 2019
1605

The script optimizes uploaded user photos to fit within the limit set by Active Directory (100 kilobytes) and best view in Adaxes (80x80 pixels).

To run the script automatically, create a Business Rule triggering Before updating a user and add the If Picture property has changed condition.

Parameters:

  • $maxWidth - specifies the maximum width of the photo in pixels;
  • $maxHeight - specifies the maximum height of the photo in pixels.
Edit Remove
PowerShell
$maxWidth = 80 # TODO: modify me
$maxHeight = 80 # TODO: modify me

function CheckFileSizeAndSave ($filePath, $quality, $imageFormat, $imageCodecInfo, $picture)
{
    if ($quality -lt 0)
    {
        DeleteTmpFile $filePath
        return
    }
    
    # Set encoder settings for image quality 
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
    $encoder = [System.Drawing.Imaging.Encoder]::Quality
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($encoder, $quality)
    
    # Save file
    $picture.Save($filePath, $imageCodecInfo, $($encoderParams))
    
    # Check file size
    $fileSize = (Get-Item $filePath).Length / 1kb
    if ($fileSize -lt 100)
    {
        return
    }
    else
    {
        $quality = $quality - 10
        CheckFileSizeAndSave $filePath $quality $imageFormat $imageCodecInfo $picture
    }
}

function DeleteTmpFile ($filePath)
{
    [System.IO.File]::Delete($filePath)
}

function UpdateUserPhoto ($tmpFilePath)
{
    if (!(Test-Path $tmpFilePath))
    {
        $Context.Cancel("Automatic photo resize failed.") # TODO: modify me
        return
    }
    
    # Update the thumbnailPhoto attribute
    $thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath)
    $Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes)
    
    # Delete the temporary file
    DeleteTmpFile $tmpFilePath
}

# Get the picture
$thumbnailPhotoBytes = $Context.GetModifiedPropertyValue("thumbnailPhoto")
if ([System.String]::IsNullOrEmpty($thumbnailPhotoBytes))
{
    return # The thumbnailPhoto attribute is empty
}

try
{
    $original = [System.Drawing.Image]$thumbnailPhotoBytes
    $originalWidth = $original.Width
    $originalHeight = $original.Height
    
    # Check original size
    $tmpFilePath = [System.IO.Path]::GetTempFileName()
    $imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
    $imageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}
    if (($originalHeight -gt $maxHeight) -or ($originalWidth -gt $maxWidth))
    {
        # Calculate the new size, preserve ratio
        $ratioX = $maxWidth / $originalWidth
        $ratioY = $maxHeight / $originalHeight
        $ratio = $ratioY
        if ($ratioX -le $ratioY)
        {
            $ratio = $ratioX
        }
       
        # Resize the picture
        $newPicture = $original.GetThumbnailImage($originalWidth * $ratio, $originalHeight * $ratio, $null, [intptr]::Zero)
        CheckFileSizeAndSave $tmpFilePath 100 $imageFormat $imageCodecInfo $newPicture
        if (!(Test-Path $tmpFilePath))
        {
            $Context.Cancel("Automatically resize photos failed.")
            return
        }
        
        UpdateUserPhoto $tmpFilePath
    }
    else
    {
        CheckFileSizeAndSave $tmpFilePath 100 $imageFormat $imageCodecInfo $original
        UpdateUserPhoto $tmpFilePath
    }
}
finally
{
    # Release resources
    if ($original) { $original.Dispose() }
    if ($newPicture) { $newPicture.Dispose() }
}

Comments ( 0 )
No results found.
Leave a comment