Compress Uploaded Images in Google Forms to Save Your Drive Space: GAS112

Introduction

If you are looking for a solution to compress the image files on your Google Drive, you may find the correct place. In this Google Apps Script project, I am sharing the core function to compress the image and a demo use case to compress the uploaded image from Google Forms.

YouTube

Check this video if you prefer to watch video instructions.

Demo

Try this demo form by uploading an image from your side to get a compressed image via email.

https://forms.gle/GdJNGBLdNMxWpe2q8

Instructions (4 Steps)

Follow the steps below to set up your own project.

Step 1

Make a copy of my project to your Google Drive with the link below.

GAS112: Compress Images in Google Forms

* Please note the linked form in my Google Sheets will not be copied to yours, so create a form on your Google Drive and link it to the copy.

Step 2

Complete the authorization requirement by clicking the button “Install trigger” from the menu “GAS112” in the copied Google Sheets.

* The linked form won’t be copied to your copy, you’ll need to create the form and link it to the copied Google Sheet.

* If you are not familiar with the authorization process, you can check this guide below.

A Complete Guide to Authorize GAS Project

Step 3

From the “GAS112” menu, install the trigger to watch the form submit events so the images uploaded from the form can be compressed to a smaller size.

* You have the option to uninstall the trigger after the trigger installation from the same menu.

Step 4 (Optional)

The image compression you can adjust in the tab “Settings”, read the notes before you make any changes.

* Don’t change the keys if you are not familiar with the script since they are used there as references.

key

value

note

size

300

The width in pixel for the compressed image

trashMe

TRUE

Trash the original image to save your space

imageField

Image

The field of the image upload in the form

imageHeader

Compressed Image

The column in the "responses" for the compressed image

errorHeader

Error Message

The column in the "responses" for the error message

emailImage

TRUE

Send the compressed image to the respondent

emailField

Email

The email field in the form for sending the image

Step 5

Submit a test response to verify the result.

The compressed image you will get from the email.

Core Functions

This is the core function which you can copy and paste into your project to use to compress an image file to a smaller size. We are utilizing the Google Drive thumbnail API to create the image size we need, per my test, the smallest width of the thumbnail we can get is 220px.

/**

* @param {GoogleAppsScript.Drive.File} file an image file

* @param {number} size The width of the image to be created

* @returns {GoogleAppsScript.Drive.File} a new image file with smaller size

*/

const compressImageFile_ = (file, size = 800) => {

 const accessToken = ScriptApp.getOAuthToken();

 const request = {

   url: `https://drive.google.com/thumbnail?id=${file.getId()}&sz=${size}`,

   headers: {

     Authorization: `Bearer ${accessToken}`,

   },

   muteHttpExceptions: true,

 };

 const [response] = UrlFetchApp.fetchAll([request]);

 if (response.getResponseCode() != 200) {

   throw new Error(response.getContentText());

 }

 const blog = response.getBlob();

 const nameSplits = file.getName().split(".");

 const name = [

   ...nameSplits.slice(0, -1).join("."),

   ` SZ${size}.`,

   ...nameSplits.slice(-1),

 ].join("");

 blog.setName(name);

 return file.getParents().next().createFile(blog);

};

GoogleAppsScript Playlist (Follow My Channel for New Projects)

Links

Hire me on Upwork (work with me) 

Donate (PayPal)

YouTube (@ashtonfei) 

YouTube (@ashtontheroad) 

Github (@ashtonfei) 

Twitter (@ashton_fei) 

Instagram (@ashton.fei) 

OneScript (my website)

Comments