Converting a picture to a jpeg so we can upload into AD

It’s about time I posted something  new – well new to this blog anyway.  I’ve been busy at work and at home too so just not found the time to update  my blog ( shame on me I know) .  I’ve even got someone following my blog too now.  It’s nice to know someone is actually reading this stuff apart from me and it’s someone I know from the Quest forums and runs her own blog which is well worth a visit.

I’ve actually been using this blog to store my little snippets of code and reuse it all from time to time.  Something I have to do soon is work out how to get my code to display nicely as the blog format is less than ideal.

Anyway lets get to the task in hand.  This will actually be a couple of posts which together will cover how to get pictures into AD so they show up in sharepoint and Outlook among other things.

My first problem was getting the pictures downloaded from a URL – I’ll post code for that later but for now I have another problem to solve, some of the pictures were the wrong size, it has to be less than 10K to upload into AD and some were in the incorrect format, i.e they were GIFs not JPEGS.

Here’s my function for converting them to JPEGS, it takes a filename and a path :

Function Convert-To-Jpeg {
 param (
  [string]$pictureFileName,
  [string]$pictureFilePath
 )
 # set a variable to hold the full path to the picture using the filename and path passed as parameters
 $pictureFullName = "$pictureFilePath\$pictureFileName"
 # check if the file laready exists, in which case we don't need to do anything
 if ( $pictureFile ) { Remove-Variable -Name $pictureFile -ErrorAction SilentlyContinue }
 try { $pictureFile = Get-Item $pictureFullName } catch { } 
 if ( $pictureFile ) {
  # check the file extension - anything other than .jpg we have to convert
  if ( $pictureFile.extension -ne "'jpg' ) {
   # set a variable to hold the target file name for when th efile is converted to a jpeg
   $convertedJpeg = $pictureFilePath+"\" + $pictureFile.Basename + ".jpg"
   # check if the target file already exists if so delete it
   if ( Test-Path $convertedJpeg ) {
    # you need to pipe thi sto out-null or the fucntion will return some additional data that you didn't expect!
    try { Remove-Item $convertedJpeg -ErrorAction SilentlyContinue  | out-null }catch { }  
   }
   # now we can convert the file at last
   [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
   if ( $bitmap ) { try { Remove-Variable -Name bitmap -ErrorAction SilentlyContinue  | out-null } catch { } }
   try { $bitmap = new-object System.Drawing.Bitmap($pictureFile.fullname) } catch { } 
   try { $bitmap.Save($convertedJpeg,"JPEG") | Out-Nill } catch { } 
   # do some variable house cleaning to be a responsible citizen 🙂
   if ( $bitmap ) { if ($bitmap -is [Disposable]) { try { $bitmap.Dispose() | Out-Null } catch { } } ; Remove-Variable -Name bitmap -ErrorAction SilentlyContinue  | out-null }
   if ( $pictureFile ) { if ($pictureFile -is [Disposable]) { try { $pictureFile.Dispose() | Out-Null } catch { } } ; Remove-Variable -Name pictureFile -ErrorAction SilentlyContinue  | out-null 
  }
 }
} # end convert-to-jpeg
  

OK next up how to shrink them

Advertisements

One thought on “Converting a picture to a jpeg so we can upload into AD

  1. […] previous posts on this topic explained how to convert the format to a  jpeg  then resize it to fit into the AD size limit and then finally how to upload it into AD in the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.