Home > Back-end >  Download HEIC file in Google Photos as JPG
Download HEIC file in Google Photos as JPG

Time:10-28

I am using this code to upload the heic file and finding a way to convert heic as jpg or png via apps script (not right click save as)

I have my code

function uploadMediaItems() {
  const albumId = "AKwCquOrmWbEihvwEqIM7Jy_H8wrkM_dof1eIGwXq8YZV-uuj9jWvkMT5oWkG6P0a-w_w9VyTOxM";  // Please set the album ID.
  var arrId =
  [
   "1ssNYJFSEdfRI55IUi_tte3iC_JcPLG-0"
  ];

  const items = arrId.map(id => {
    const file = DriveApp.getFileById(id);
    return {blob: file.getBlob(), filename: file.getName()};
  });
  const res = GPhotoApp.uploadMediaItems({albumId: albumId, items: items});
  console.log(JSON.stringify(res))
}

Source: Google Apps Scripts: import (upload) media from Google Drive to Google Photos?

CodePudding user response:

I believe your goal is as follows.

  • You want to upload the HEIC file to Google Photos and export it as a Jpeg or PNG file using the sample script at this thread.
  • From your script, the HEIC file is put on your Google Drive.

I thought that in your goal, you might have wanted to convert the HEIC file to Jpeg or PNG file. In this case, how about the following sample scripts?

Sample script 1:

In this sample script, the sample script at this thread is used. So, in this case, a Google Apps Script library of GPhotoApp is used. Ref The sample script is as follows.

In this script, it supposes that your Google Apps Script project has already been linked with Cloud Platform Project, and Photos API has already been enabled, and also, the required scopes have already been added. Please be careful this.

function myFunction() {
  const arrId = ["###"]; // Please set the file ID of your HEIC file.
  const albumId = "###"; // Please set your albumn ID.

  const items = arrId.map(id => {
    const file = DriveApp.getFileById(id);
    return { blob: file.getBlob(), filename: file.getName() };
  });
  const res1 = GPhotoApp.uploadMediaItems({ albumId: albumId, items: items });
  const mediaItemId = res1.newMediaItemResults[0].mediaItem.id;
  const res2 = GPhotoApp.getMediaItems({ mediaItemIds: [mediaItemId] });
  const mediaItem = res2.mediaItemResults[0].mediaItem;
  const blob = UrlFetchApp.fetch(mediaItem.baseUrl).getBlob();
  DriveApp.createFile(blob.setName(mediaItem.filename.replace(".heic", ".png")));
}
  • When this script is run, the PNG file is created to the root folder.

Sample script 2:

The HEIC file is put on your Google Drive. And, you want to convert the HEIC file to Jpeg or PNG. In this case, I thought that this can be achieved using the following simple script. In this case, Photos API is not required to be used.

When you use this script, please enable Drive API at Advanced Google services.

function myFunction2() {
  const fileId = "###"; // Please set the file ID of your HEIC file.

  const obj = Drive.Files.get(fileId);
  const blob = UrlFetchApp.fetch(obj.thumbnailLink.replace(/=s. /, "=s2000")).getBlob();
  DriveApp.createFile(blob.setName(obj.title.replace(".heic", ".png")));
}
  • When this script is run, the PNG file is created to the root folder.

References:

  • Related