-2

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?

user16731842
  • 83
  • 1
  • 10
  • 2
    So what exactly is your question here? – ale13 Oct 26 '21 at 14:00
  • At first, I deeply apologize that my sample script was not useful for your situation. For your question, I proposed an answer. Could you please confirm it? If that was not useful, I apologize again. – Tanaike Oct 27 '21 at 00:03
  • You are a great man, and its me who need to apologise for my poor explanation. Your answer is absolutely correct to the use case. This use case is something related to HEIC. I am glad that you have correctly found a way to do it. I really appreciate Tanaike – user16731842 Oct 27 '21 at 05:10

1 Answers1

3

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165