Using Zapier and TinyPNG to automatically compress your images
The Zapier integration is currently in beta. To follow this guide, please join the beta!
Zapier makes it super simple to connect your apps to the TinyPNG API, allowing anyone to automatically optimise their images.
The TinyPNG and TinyJPG developer API allows developers to optimise their images from their own app or service. To make the API more accessible for users with no programming experience, we launched an integration with Zapier. In this blog post we will create an example Zap that automatically compresses images inside a Dropbox folder, but it will be very similar for other cloud services supported by Zapier, such as Google Drive, OneDrive or Box.
Zapier is an online service that allows you to connect apps together by creating automated workflows. If you're already familiar with it, you can safely skip to the next section.
To automate a process you can create Zaps. Zaps are blueprints for automated tasks, that consist of triggers and actions. An example Zap could be:
When a new image is uploaded to Dropbox, compress the image with TinyPNG.
Zaps can be very simple (when A do B), but it is possible to add multiple actions and filters in between. This makes Zapier a very powerful tool, which we will illustrate with the example in the following section.
Compressing new Dropbox images
First, we need to sign up for an account for the TinyPNG API. When you enter your details on the website, you will receive a link to your API dashboard. The account is free and allows to compress 500 images each month. More images can be compressed for a small fee per image.
Creating the trigger
The next step is creating the Zap. In the new Zap, select Dropbox as the trigger app and choose the New file in directory trigger. On the next page, you will be prompted to connect your Dropbox account. Connect your account and continue to the next step. Now, we need to configure the folder in which the images will be compressed. For this example, we created a new folder.
Finally, the trigger needs to be tested. Upload an image to the previously selected folder and check if it can be found during the testing step.
Adding the compress action
After setting up the trigger, we will add an action that compresses the image. Select the TinyPNG app and choose the Compress Image action. Next, connect your API account by filling in your key found on the API dashboard. If you can't find the TinyPNG app, make sure to join the beta first.
When your account was succesfully connected, we need to specify the image that will be compressed. Select the File from step 1, by clicking the Insert button on the right side of the field. Continue to the next step and see if the test is successful.
Saving the compressed image
Finally, we need to add another action that saves the compressed image back to Dropbox. Choose Dropbox as the action app and pick Upload File. Choose the Dropbox account from step 1 and setup the options as follows:
- Directory: target location for the compressed image, can be the same as the source directory.
- File: select the Output File from step 2.
- Overwrite: choose Yes when you picked the same source and target directory.
- Specify File Name: select File Name from step 1.
- Specify File Extension: select File Extension from step 1 (prevents renaming JPEG to JPG).
If you correctly filled all the fields you can test the last step and check the file size of the updated image in your Dropbox folder. If everything is working as it should, you can name your Zap and enabled it.
Filtering unsupported files
The Zap created in the previous steps will try to compress all your files in the specified folder. As only PNG and JPG files are supported by the API, other file types will generate errors in the execution of the Zap.
To prevent these errors, we can insert a filter step between the trigger and the first action. This filter will make sure the Zap will only try to compress supported files. Configure it to only continue if the File Ext text exactly matches
.png. Click the OR button to create a similar rule for
.jpg and for
.jpeg. After the filter is succesfully tested, don't forget to re-enable the Zap.
Using Zapier we can automate tasks between different web apps and services. Zapier consists of triggers, filters and actions that can be use to build a Zap. For the TinyPNG API, we created an action that allows to compress your images with no programming knowledge required.
In this blog post, we created Zap with this action that monitors a Dropbox folder and compresses newly uploaded images. A very similar approach can be used to integrate the API with other cloud services supported by Zapier, such as Google Drive, OneDrive or Box.