We are trying to mass change user photos through a scripted method. We are using a modified version of the Import User Photo and Optimize User Photo scripts. Optimization script is posted below the post.
The import script is configured as a custom command.
The optimize script as a business rule to run before updating a user if the jpegPhoto attribute has changed.
When I edit the jpegPhoto manually the business rule and script perform as expected.
-
Get the modified bytes of jpegPhoto.
-
Check dimensions and byte count.
- Scale and reduce quality if needed.
-
Create thumbnailPhoto
- Reduce quality if needed.
-
Set jpegPhoto and thumbnailPhoto
When I run the Import user photo script targeted at a user, the imported photo is not scaled and quality is not reduced.
However, the thumbnail is created and saved.
Any ideas?
## Contants
$maxThumbnailDimension = 300
$maxThumbnailKB = 100
$maxJpegDimension = 1200
$maxJpegKB = 600
## Functions
function DeleteTmpFile ($filePath)
{
[System.IO.File]::Delete($filePath)
}
function CheckFileSizeAndSave ($filePath, $quality, $imageFormat, $imageCodecInfo, $maxBytes, $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 $maxBytes)
{
return
}
else
{
$quality = $quality - 5
CheckFileSizeAndSave $filePath $quality $imageFormat $imageCodecInfo $maxBytes $picture
}
}
function UpdateUserPhotos ($jpegPhotoPath, $thumbnailPhotoPath)
{
if (!(Test-Path $thumbnailPhotoPath))
{
$Context.Cancel("Automatic thumbnail creation failed.")
return
}
# Update the thumbnailPhoto attribute
$jpegPhotoBytes = [System.IO.File]::ReadAllBytes($jpegPhotoPath)
$Context.SetModifiedPropertyValue("jpegPhoto", $jpegPhotoBytes)
$gamOutput = & "c:\gam\gam.exe" user '%mail%' update photo $jpegPhotoPath 2>&1
if ($gamOutput -like "*Error*"){
Write-Error "$gamOutput"
}else{
$Context.LogMessage("%mail% profile photo has been updated.", "Information")
}
# Update the thumbnailPhoto attribute
$thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($thumbnailPhotoPath)
$Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes)
# Delete the temporary file
DeleteTmpFile $thumbnailPhotoPath
DeleteTmpFile $jpegPhotoPath
}
$jpegPhotoBytes = $Context.GetModifiedPropertyValue("jpegPhoto")
if ([System.String]::IsNullOrEmpty($jpegPhotoBytes))
{
return # The jpegPhoto attribute is empty
}
try
{
$original = [System.Drawing.Image]$jpegPhotoBytes
$originalWidth = $original.Width
$originalHeight = $original.Height
$originalRatio = $originalWidth / $originalHeight
if ($originalRatio -ne 1) {
$Context.Cancel("Please upload a square ratio (1:1) photo.")
}
if ($originalHeight -lt $maxJpegDimension) {
$Context.Cancel("Please upload a photo that is at least $maxJpegDimension x $maxJpegDimension.")
}
$jpegPhotoFilePath = [System.IO.Path]::GetTempFileName()
$thumbnailPhotoFilePath = [System.IO.Path]::GetTempFileName()
$imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
$imageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}
# Check original size
if ($originalHeight -gt $maxJpegDimension)
{
# Resize the picture
$newJpegPicture = $original.GetThumbnailImage($maxJpegDimension, $maxJpegDimension, $null, [intptr]::Zero)
CheckFileSizeAndSave $jpegPhotoFilePath 100 $imageFormat $imageCodecInfo $maxJpegKB $newJpegPicture
if (!(Test-Path $jpegPhotoFilePath))
{
$Context.Cancel("Automatically resize jpeg failed.")
return
}
}
else
{
CheckFileSizeAndSave $jpegPhotoFilePath 100 $imageFormat $imageCodecInfo $maxJpegKB $original
}
$newThumbnailPicture = $original.GetThumbnailImage($maxThumbnailDimension, $maxThumbnailDimension, $null, [intptr]::Zero)
CheckFileSizeAndSave $thumbnailPhotoFilePath 100 $imageFormat $imageCodecInfo $maxThumbnailKB $newThumbnailPicture
if (!(Test-Path $thumbnailPhotoFilePath))
{
$Context.Cancel("Automatically resize thumbnail failed.")
return
}
UpdateUserPhotos $jpegPhotoFilePath $thumbnailPhotoFilePath
}
finally
{
# Release resources
if ($original) { $original.Dispose() }
if ($newJpegPicture) { $newJpegPicture.Dispose() }
if ($newThumbnailPicture) { $newThumbnailPicture.Dispose() }
}