0 votes

Hello!

We have allowed our users to update their own photo through the Adaxes web interface. Of course we have also hinted at the optimum dimensions and size, and of course this has been completely ignored. Many of the pictures I see being added are really large, the largest I've found was 722x1101 at 1.5MB, and with 2000 users in our AD I don't know if we are going to run into problems once more people use this feature.

Is there any way to limit file size and/or dimensions?

Best regards

by (160 points)
0

Hello,

Yes, it is possible, for example, to create a Business Rule triggered before updating a user's photo that will cancel the operation if the photo exceeds the limit (either the file size limit or the dimensions limit). This will require a PowerShell script to accomplish. We've asked our script guys to come up with something and will update this topic as soon as they have some results.

0

That's excellent news, thank you!

0

Hi,

Do you have any news concerning this?

Thanks
Erik

1 Answer

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

Hello Erik,

Yes, of course. It will be impossible to cancel the operation if the photo is too large, however it is possible to automatically resize the photo to the required size when users upload their photos. For this purpose, you'll need to crate a Business Rule triggered after updating the Picture attribute to automatically resize the uploaded photo. To create such a Business Rule:

  1. Create a new Business Rule.

  2. On the 2nd step of the Create Business Rule wizard, select User and After Updating a User.

  3. On the 3rd step, add the Run a program or PowerShell script action and paste the following script in the Script field:

     $maxWidth = 80 # TODO: modify me
     $maxHeight = 80 # TODO: modify me
    
     # Get the picture
     try
     {
         $thumbnailPhotoBytes = $Context.TargetObject.Get("thumbnailPhoto")
     }
     catch
     {
         return # the thumbnailPhoto property is empty
     }
     $original = [System.Drawing.Image]$thumbnailPhotoBytes
     $originalWidth = $original.Width
     $originalHeight = $original.Height
    
     # Check original size
     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)
         $tmpFilePath = [System.IO.Path]::GetTempFileName()
         $newPicture.save($tmpFilePath)
         $thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath)
    
         # Delete the temporary file
    
         # Update thumbnailPhoto property
         $Context.TargetObject.Put("thumbnailPhoto", $thumbnailPhotoBytes)
         $Context.TargetObject.SetInfo()
    
         $newPicture.Dispose()
     }
    
     $original.Dispose()
    
  4. In the script, $maxWidth and $maxHeight specify the maximum width and height of the picture allowed. Modify them to meet your requirements.

  5. Enter a short description for the script and click OK.

  6. Now, you need to specify conditions for the script to be run only when a user's picture is modified. To do this. right-click the action you've just added and click Add Condition.

  7. Select the If <property> changed condition type.

  8. Specify If Picture has changed.

  9. Click OK.

  10. Finish creation of the Business Rule.

0

Thanks for that, looks great! I'll give it a try as soon as possible.

OK I tried it, leaving the maximum dimensions at 80x80 px. When I apply the change in the user properties it gives me the error

A value for the attribute was not in the acceptable range of values. (Server: <our domain>)

Any suggestions?

Best regards
Erik

0

Hello Erik,

This occurs because the file that you are trying to upload exceeds the limit of 100 kB allowed by Active Directory. To workaround this, you'll need to resize the picture before updating the Picture attribute so that it does not exceed the limit. Since the script will resize the picture anyway, you can remove the Business Rule triggered per our previous instructions and replace it with a Business Rule created per the instructions below:

  1. Create a new Business Rule.

  2. On the 2nd step of the Create Business Rule wizard, select User and Before Updating a User.

  3. On the 3rd step, add the Run a program or PowerShell script action and paste the following script in the Script field:

     $maxWidth = 80 # TODO: modify me
     $maxHeight = 80 # TODO: modify me
    
     # Get the picture
     $thumbnailPhotoBytes = $Context.GetModifiedPropertyValue("thumbnailPhoto")
     if ([System.String]::IsNullOrEmpty($thumbnailPhotoBytes))
     {
         return # the thumbnailPhoto property is empty
     }
    
     $original = [System.Drawing.Image]$thumbnailPhotoBytes
     $originalWidth = $original.Width
     $originalHeight = $original.Height
    
     # Check original size
     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)
         $tmpFilePath = [System.IO.Path]::GetTempFileName()
         $newPicture.save($tmpFilePath)
         $thumbnailPhotoBytes = [System.IO.File]::ReadAllBytes($tmpFilePath)
    
         # Delete the temporary file
    
         # Update thumbnailPhoto property
         $Context.SetModifiedPropertyValue("thumbnailPhoto", $thumbnailPhotoBytes)
    
         $newPicture.Dispose()
     }
    
     $original.Dispose()
    
  4. In the script, $maxWidth and $maxHeight specify the maximum width and height of the picture allowed. Modify them to meet your requirements.

  5. Enter a short description for the script and click OK.

  6. Now, you need to specify conditions for the script to be run only when a user's picture is modified. To do this, double-click Always.

  7. Select the If <property> changed condition type.

  8. Specify If Picture has changed.

  9. Click OK.

  10. Finish creation of the Business Rule.

0

That works just fine. Thanks for your effort!

Best regards
Erik

0

Hi Support,

is this still the only way to automatically resize pictures even in Adaxes 2023?

Thanks

0

Hello Boris,

Yes, that is correct. using a business rule and a script is the only way.

Related questions

0 votes
1 answer

I know the question is related to an older version, but where do I find the "Thumbnail Photo Part" in Permissions? The task is: A department should be able to change the Userpicture, nothing else. Thanks

asked Nov 24, 2022 by boris (530 points)
0 votes
1 answer

Hello! Is there a way to clear the AD attribute "thumbnailPhoto" with a checkbox property? I wanted to implement a checkbox in the mask "Modify user" and if the box is checked, clear the attribute. How do I implement this? Much appreciated, Marco

asked Apr 21, 2022 by marco_jandl (60 points)
0 votes
1 answer

I know this is linked to an older version of the interface, but is possible to disable editing thumbnail photos on all web interfaces? We have a workflow to to edit the ... high quality photo which is resized for the thumbnail and also pushed to a Cloud IdP.

asked Sep 15, 2020 by polley (1.2k points)
0 votes
1 answer

Is it possible to create a business unit and have it auto populate with group owners in a specific OU. I've tried a few scripts to get propertie adm-managedbylist but none have worked so far.

asked 5 days ago by C27 (20 points)
0 votes
1 answer

I am trying to have a scheduled job that will hide groups that are empty and I can not seem to figure out how to do it.

asked Aug 20, 2021 by hgletifer (1.3k points)
3,548 questions
3,239 answers
8,232 comments
547,814 users