Created
October 6, 2019 11:50
-
-
Save NotNotWrongUsually/7db8f3f7abd88a95868284652232b9af to your computer and use it in GitHub Desktop.
Grab image from AD and rescale if desired
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function UserPhoto ($user, [int]$width, [int]$height) { | |
$user_data = ([adsisearcher]"(&(objectClass=user)(sAMAccountName=$user))").FindAll() | |
$stream = [System.Io.MemoryStream]::new($user_data.Properties["thumbnailphoto"][0]) | |
$img = [System.Drawing.Image]::FromStream($stream) | |
if ($width -or $height) { | |
if (-not $width) { | |
$width = ($img.Width / $img.height) * $height | |
} | |
if (-not $height) { | |
$height = ($img.height / $img.width) * $width | |
} | |
if ($width -ne $img.Width -or $height -ne $img.Height) { | |
$destination_rect = New-Object System.Drawing.Rectangle 0, 0, $width, $height | |
$destination_image = New-Object System.Drawing.Bitmap $width, $height | |
$destination_image.SetResolution($img.HorizontalResolution, $img.VerticalResolution) | |
$graphics = [System.Drawing.Graphics]::FromImage($destination_image) | |
$graphics.CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceCopy | |
$graphics.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality | |
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality | |
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality | |
$wrapmode = [System.Drawing.Imaging.ImageAttributes]::new() | |
$wrapmode.SetWrapMode([System.Drawing.Drawing2D.WrapMode]::TileFlipXY) | |
$graphics.DrawImage($img, $destination_rect, 0, 0, $img.Width, $img.Height, [System.Drawing.GraphicsUnit]::Pixel, $wrapmode) | |
$destination_image | |
} | |
else { | |
# Width and height parameters don't differ from what the image already has | |
$img | |
} | |
} | |
else { | |
$img | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment