Search code examples
google-apps-scriptgoogle-drive-apijpeggoogle-formsheic

How to convert .heic to .jpeg from Google Form before uploading into Google Drive using Google Apps Script


This is my first time programming with Google products so please bear with me.

I created a Google Form that accepts photos as file input and places them in Google Drive in a target folder determined by the form responses.

I started with this resource: https://developers.google.com/apps-script/samples/automations/upload-files#code.gs

My current code works fine, placing the images in the correct target folder.

I would like to convert .heic files to .jpeg so all devices can open the files. I am trying to use the function getAs("image/jpeg") for the conversion.

If I leave out the getAs() function, the .heic files get placed in the correct target folder.

When I use the getAs() function, the .heic file is placed in the default Files (File response) folder but no .jpeg is created and nothing is put in the target folder.

Note that once I figure out how to do that, I will use an if/else statement to apply the conversion to .heic files only.

Thanks in advance for your help!

...

function onFormSubmit(e) {

  // Get the destination folder
  var destFolder = folder_based_on_form_response;  // this part works

  // Get all form responses
  let itemResponses = e.response.getItemResponses();

  // Get the file upload response as an array to allow for multiple files
  let fileUploads = itemResponses.filter((itemResponse) => itemResponse
    .getItem().getType().toString() === "FILE_UPLOAD")
    .map((itemResponse) => itemResponse.getResponse())
    .reduce((a, b) => [...a, ...b], []);

  // Move the files to the destination folder
  if (fileUploads.length > 0) {
    fileUploads.forEach((fileId) => {
    DriveApp.getFileById(fileId).getAs("image/jpeg").moveTo(destFolder);
    });
  }
}

...


Solution

  • I believe your goal is as follows.

    • You want to convert .heic format to Jpeg format by modifying your script.

    In this case, how about the following modification? In this modification, the following flow is used.

    1. Detect .heic file.
    2. Convert .heic format to PNG format.
    3. Convert PNG format to JPEG format.
    4. Create the image data in the JPEG format in the destination folder as a file.

    Modified script:

    From:

    if (fileUploads.length > 0) {
      fileUploads.forEach((fileId) => {
      DriveApp.getFileById(fileId).getAs("image/jpeg").moveTo(destFolder);
      });
    }
    

    To:

    if (fileUploads.length > 0) {
      fileUploads.forEach((fileId) => {
        const file = DriveApp.getFileById(fileId);
        const name = file.getName();
        const mimeType = file.getMimeType();
        if (name.includes("heic") || name.includes("heif") || mimeType == "image/heic") {
          try {
            const url = `https://drive.google.com/thumbnail?id=${fileId}&sz=w1000`;
            const blob = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob().getAs("image/jpeg");
            destFolder.createFile(blob.setName(name.split(".")[0] + ".jpg"));
    
            // file.setTrashed(true); // If you want to remove the original file, please use this.
          } catch (e) {
            console.log(e); // If an error occurs, you can see it in the log.
          }
        } else {
          file.moveTo(destFolder); // or file.getAs("image/jpeg").moveTo(destFolder);
        }
      });
    }
    
    • When this script is run when the file is .heic file, the .heic file is converted to Jpeg format and moved to the destination folder.

    Note:

    • When I tested this script using sample .heic files, no error occurs. The .heic files could be converted to Jpeg format and moved to the destination folder. But, if your .heic files are files that cannot be converted, an error occurs. please be careful about this.
    • This script requires an "On Form Submit" installable trigger.