Compress and resize images when they are uploaded to SharePoint

This article describes how to create a SharePoint Workflow which will automatically resize an image when a user saves it into the document library. We will create a workflow which not only resizes the image but also converts it if the source image is in the uncompressed format. Also our Workflow will get the EXIF data from the source file and add to the document library’s field.

To show you how the Workflow works we have created a diagram in Microsoft Visio. We will not use Microsoft Visio to create this workflow because it is not a complex workflow and can be created in SharePoint Designer directly. To look how the Workflow looks in Microsoft Visio click the following picture:

 


Click to open real size screenshot

The Workflow works in following manner. At first it detects a format of an uploaded file and if it is an image — the file will be taken into the process.

For the processed file it will get the EXIF data (we will get details on which a digital camera was used to shot the image), detect if the image is in the compressed format (JPG, GIF) or not (TIFF, BMP) and convert/resize the image if it is necessary.

Such Workflow cannot be created using own SharePoint Workflow conditions and actions and we will use several components from HarePoint Workflow Extension. The list of used components is the following:

Component name and type Set of Workflow components
SharePoint Workflow HarePoint Workflow Extensions
Is the Image type supported (condition)
Get EXIF property from Image (Action)
Resize Image (Action)
Is the Image compressed (Condition)
Update List Item (Action)
Convert Image (Action)
Delete Item (Action)

Let's start the Workflow creation.

  1. Create a new document library or modify existing one. It is necessary to add one additional field where a Workflow will save the information about the model of the camera used to take a photo. Name this filed as “Camera Model”.
  2. Start SharePoint Designer, open the web-site, point to “Workflows”, click the “List Workflow” button on the ribbon and select the Document Library to which the Workflow will be linked (the Document Library which we created or modified at the Step 1). Type a name of the Workflow. Click OK. The empty workflow has been created, it should look like:

     


    Click to open real size screenshot

  3. We should add a condition, which defines the format of the document added and either leaves the document in the current format or processes it. To do this click the “Condition” button on the ribbon and select the “Is Image type supported (HarePoint)” condition. This condition requires entering of the Full URL of the picture in the Document Library. To set it click the “this item url” link:

     

    and click the “Fx” button:

     

    Set “Current item” in the Data source field and “Encoded Absolute URL” in the “Field from source” field:

     

    Click OK. Condition which defines the format of the document added into the Document Library is configured. It should look like:

     

  4. If the document is an image, we obtain the information about shooting conditions. To do this, we should add the Action “Get EXIF property from the image”. This action is located in the Images section in the list of Actions:

     

    For this action we should select the field containing camera information, specify the URL of the picture in the document library from which the data should be obtained and also we should specify a variable to which the data will be assigned (we will name the variable “Camera Model”). After setting all these parameters the action should look like:

     


    Click to open real size screenshot

    At the end of the Workflow we will add data from the Camera Model variable to the Document Library field.

  5. Our next step is to check the image size. We don’t want to upload large files to the Document Library, so we add an Action “Resize Image”, which allows us to reduce an image size.

    For this action we should specify the URL of the image in the Document Library, set the values of the width and the height to which the image must be resized (it is possible to keep an aspect ratio). Also we should specify what must be done with resized image: overwrite the source image or add a new image with “_resize” at the end of file name. The last parameter defines what the action must to do if the image is smaller than the target file. We will resize the image to 800 x 600 pixels with keeping the aspect ratio, overwrite the existing file and do not process the file if it is smaller than the target file:

     


    Click to open real size screenshot

  6. If you will try to upload a large file, for example, a TIFF image, the Workflow must convert it to an optimized JPG image. Now we will check whether this image is compressed. To do this we should add the “Is Image Compressed (HarePoint)” condition. This condition requires specifying the only parameter: the url to the image in the Document Library. The configured component looks like:

     


    Click to open real size screenshot

  7. If the file is already compressed, we will just have to add camera model information to a new Document Library element. We can do this using the “Update List Item at URL” action. For this action we should specify which field we should update and set a data which will be entered into this field. Click the “specified values” link in the action parameter and set the value like this one:

     

    To add the value [%Variable: Camera Model%] click the “Add or Change Lookup” button and set the following values:

     

    We specified that we want to update the Camera Model column and set its value to that of the variables we had obtained from EXIF at the Step 4. In the settings we place a link to the image that needs to be added. The configured action looks like:

     


    Click to open real size screenshot

  8. Type “Else” to specify which actions should be executed if the image has not been compressed.
  9. If the image has not been compressed, its format has to be changed. To do this, we use the “Convert Image” action. In the settings we should specify the url of the image in the Document Library that has to be converted. Also we should set the new required format. Two other parameters which should be set up are: the variable which will keep the url to the converted image and what the action must to do if the file exists: overwrite or not. Configured action looks like:

     


    Click to open real size screenshot

    After that we will have two files, and the source file has to be deleted in order not to leave excessive files on the SharePoint.

  10. To delete the source file we should add the “Delete Item” action and specify what should be deleted:

     

    Now we have only one JPG image with the size 800x600 pixels.

  11. For the new file we have to update the “Camera Model” field using the EXIF data obtained earlier. We already did it at the Step 7 but the values of parameters should be changed to:

     


    Click to open real size screenshot

  12. The Workflow is ready, it looks like:

     


    Click to open real size screenshot

  13. Save the Workflow.
  14. At last we should set the Workflow to launch automatically when new files have been uploaded to the Document Library. To do this click the “Workflows” link in the left menu of SharePoint Designer, select your Workflow and double click it. Check with a tick the option “Start workflow automatically when an item is created” and click the “Save” button on the ribbon.
  15. Publish the Workflow. To do this click the “Publish” button on the SharePoint Designer ribbon.
  16. Test the Workflow. Try to upload a TIF image into the document library and make sure that the image is resized and converted to the JPG format. Also the “Camera Model” column in the Document Library contains the data about the digital camera which has been used to shot the image.

You can also look at the Video Demo the whole process of creating such a workflow.

​​​

Our valued customers

HarePoint products are recognized in thousands companies

  • Ofgem
  • Fair Work Australia
  • Air Caraïbes
  • Jones Lang LaSalle
  • Delta Solutions
Search