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

Hi team, I have a follow up to this question https://www.adaxes.com/questions/14234/business-after-adding-members-powershell-script-executed Let me explain my setup A rule- ... area% failed due to the following exception: $($_.Exception.Message)", "Error") }

asked Feb 13 by wintec01 (1.1k points)
0 votes
1 answer

Hi, I have a business rule setup to perform actions after user creation. First action is to run a powershell script which works and it sets a required AD attribute ( ... new user sits in the original OU and does not move Am i missing something here?

asked Feb 6 by Lewis (40 points)
0 votes
1 answer

Hi, I need to retreive a secret from a Azure Keyvault in a business rule. I have a powershell script that works if i run a external command. But it fails if ... at <ScriptBlock>, <No file>: line 20 Any sugestion? Kind regards Reidar Dick-Henriksen

asked Dec 6, 2023 by reidardh (20 points)
0 votes
1 answer

I am trying to trigger processing outside of Active Directory when an account is created based on the source user account that was used. Does Adaxes store the source account anywhere?

asked Oct 9, 2023 by jnordell (20 points)
3,326 questions
3,026 answers
7,727 comments
544,678 users