0 votes

Hi,
I would like to launch a script to optimize photo to 96x96 when photo is updated.
I use a command line to do the job but i can figure out how to get the photo before the user update. Any chance Adaxes stores the photo in a temp folder before it loads it to AD ?

Thanks

Stephen

by (800 points)

1 Answer

0 votes
by (18.0k points)
selected by
Best answer

Hi Stephen,

Adaxes doesn't store photos in temporary files. However, you can do it by yourself with the help of a PowerShell script.
The following script saves the photo to a temporary file, executes an external command for this file, and then replaces the user-specified photo with the modified one.

if (-not $Context.IsPropertyModified("thumbnailPhoto"))
{
    return; # the thumbnailPhoto property is not modified
}
# Get the thumbnailPhoto
$thumbnailPhotoBytes = $Context.GetModifiedPropertyValue("thumbnailPhoto");
if ($thumbnailPhotoBytes -eq $NULL)
{
     return; # the thumbnailPhoto property is being deleted
}

# Write the photo to temporary file
$tmpFilePath = [System.IO.Path]::GetTempFileName();
[System.IO.File]::WriteAllBytes($tmpFilePath, $thumbnailPhotoBytes);

# TODO: Launch an external command to resize the photo ($tmpFilePath)

# Read the modified file
$thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath);
# Delete the temporary file
[System.IO.File]::Delete($tmpFilePath);

# Replace the photo with the modified one
$Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes);
0

You're a genius :-)

Thanks a lot !

0

Glad to help :)

0

This is exactly what I've been looking for...I'm using ImageMagik via command line now to shrink/crop/resize our user images for AD. When I add the command line option in the field, I receive an error when attempting to do the above. I think my problem is the fact that the images we have are 1MB + in size. When we use the command line it shrinks all the files properly etc. But i can't seem to figure out how to get it to work with this script.

if (-not $Context.IsPropertyModified("thumbnailPhoto"))
{
    return; # the thumbnailPhoto property is not modified
}
# Get the thumbnailPhoto
$thumbnailPhotoBytes = $Context.GetModifiedPropertyValue("thumbnailPhoto");
if ($thumbnailPhotoBytes -eq $NULL)
{
     return; # the thumbnailPhoto property is being deleted
}

# Write the photo to temporary file
$tmpFilePath = [System.IO.Path]::GetTempFileName();
[System.IO.File]::WriteAllBytes($tmpFilePath, $thumbnailPhotoBytes);

# TODO: Launch an external command to resize the photo ($tmpFilePath)
"C:\Adaxes\Scripts\Convert\Convert.exe -quality 96 -depth 8 -strip -thumbnail 96x96^ -gravity Center -crop 96x96+0+0 $tmpFilePath\$thumbnailPhotoBytes $tmpFilePath\Converted.jpg"
# Read the modified file
$thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath);
# Delete the temporary file
[System.IO.File]::Delete($tmpFilePath);

# Replace the photo with the modified one
$Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes);

0

It would be awesome if the pics got resized automatically when imported from the web portal! :)

0

Hello,

As to the script, we've assigned the task to our script guy. We'll update this topic as soon as he comes up with something.

As to resizing pictures on import, we already have this feature in our TODO list. We'll consider it for one of the future releases.

0

Hello,

Here's the modified script that should do the job.

if (-not $Context.IsPropertyModified("thumbnailPhoto"))
{
    return; # the thumbnailPhoto property is not modified
}
# Get the thumbnailPhoto
$thumbnailPhotoBytes = $Context.GetModifiedPropertyValue("thumbnailPhoto");
if ($thumbnailPhotoBytes -eq $NULL)
{
     return; # the thumbnailPhoto property is being deleted
}

# Write the photo to temporary file
$tmpFilePath = [System.IO.Path]::GetTempFileName();
[System.IO.File]::WriteAllBytes($tmpFilePath, $thumbnailPhotoBytes);

# TODO: Launch an external command to resize the photo ($tmpFilePath)
& "C:\Adaxes\Scripts\Convert\Convert.exe" "$tmpFilePath" -quality 96 -depth 8 -strip -thumbnail 96x96^ -gravity Center -crop 96x96+0+0 "$tmpFilePath" 

# Read the modified file
$thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath);
# Delete the temporary file
[System.IO.File]::Delete($tmpFilePath);

# Replace the photo with the modified one
$Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes);

The thing is that whenever you launch an executable file in PowerShell, it should be preseed with an & symbol. Otherwise PowerShell will treat the line as a PowerShell command.

Also, you saved the converted file to $tmpFilePath\Converted.jpg, but tried to upload to directory not the converted file, but the original source file before conversion.

Related questions

0 votes
1 answer

This is the query I am using (basically if "Photo" is empty): (&(sAMAccountType=805306368)(!(photo=*))) which returns everyone in AD, not just users without photos. ... sAMAccountType=805306368)(!(manager=*))) What am I doing wrong? Can my query be fixed?

asked Jun 11, 2012 by MarkManley (90 points)
0 votes
1 answer

I have a 'Before user account creation' business rule that starts as follows: If script 'Validate user exists' returns true then Do Nothing // rest of ... experiencing a bug where $Context.Cancel is not properly terminating the business rule. Thanks.

asked Dec 17 by lw.fa (150 points)
0 votes
1 answer

In a business rule, I'd like to pass Adaxes variables into a powershell script that I'll run. For example, pass %username% into the script so it can be used inside the script.

asked Sep 5 by P-Sysadmin (20 points)
0 votes
1 answer

The closest operation I figure is "modfying exchange properties" of a user but not sure how I can filter the conditions to match. It's possible I could ... ? Was also looking at adm-MsExchMailboxCalendarPermissions but havent played around with that yet.

asked May 17 by ZoomGhost (280 points)
0 votes
1 answer

Rule 1. we have a business rule which disables a user account after updating a user. It then does some other actions. Rule 2. we have a business rule which performs ... 2 then triggered immediately and the flow of control handed back to rule 1 to continue?

asked Apr 3 by i*windows (300 points)
3,589 questions
3,278 answers
8,303 comments
548,130 users