Script for inserting Drive images into Google Slides: Help needed

Hey everyone! I’m stuck with a problem. I’m trying to add a JPG image from my Google Drive to a Google Slides presentation using a script. The image is under 50 MB, but I keep getting an error that says the image is invalid or corrupt.

Here’s the code I’m using:

function insertDriveImageToSlides() {
  const slideshow = SlidesApp.openById('SLIDESHOW_ID');
  const newSlide = slideshow.addSlide(SlidesApp.PredefinedLayout.BLANK);

  const driveImageId = 'IMAGE_ID';
  const downloadUrl = `https://drive.google.com/uc?export=download&id=${driveImageId}`;
  const imageBlob = UrlFetchApp.fetch(downloadUrl, { headers: { authorization: 'Bearer ' + ScriptApp.getOAuthToken() } }).getBlob();

  try {
    const insertedImage = newSlide.insertImage(imageBlob);

    const slideWidth = slideshow.getPageWidth();
    const slideHeight = slideshow.getPageHeight();

    insertedImage.setLeft(slideWidth / 2 - insertedImage.getWidth() / 2);
    insertedImage.setTop(slideHeight / 2 - insertedImage.getHeight() / 2);

  } catch (error) {
    console.log('Error occurred: ', error);
  }
}

I’ve tried using the image thumbnail, which works but the quality is very poor. What could be causing this, and how can I fix it? Thanks in advance for your help!

I’ve encountered similar issues when working with Drive images in Google Slides scripts. One thing that worked for me was using the advanced Drive API instead of the UrlFetchApp method. Here’s a modified version of your script that might help:

function insertDriveImageToSlides() {
  const slideshow = SlidesApp.openById('SLIDESHOW_ID');
  const newSlide = slideshow.addSlide(SlidesApp.PredefinedLayout.BLANK);

  const driveImageId = 'IMAGE_ID';
  const file = DriveApp.getFileById(driveImageId);
  const blob = file.getBlob();

  try {
    const insertedImage = newSlide.insertImage(blob);

    const slideWidth = slideshow.getPageWidth();
    const slideHeight = slideshow.getPageHeight();

    insertedImage.setLeft(slideWidth / 2 - insertedImage.getWidth() / 2);
    insertedImage.setTop(slideHeight / 2 - insertedImage.getHeight() / 2);

  } catch (error) {
    console.log('Error occurred: ', error);
  }
}

This approach uses DriveApp to directly fetch the file blob, which should bypass any potential issues with the download URL. Make sure you have the necessary permissions set up in your script project. If you’re still encountering problems, double-check that the image file itself isn’t corrupted in Drive.

hey mike, hav you tried using the DriveApp.getFileById() method? it worked for me when i had similar issues. just make sure ur script has the right permissions for Drive access. if that doesnt work, maybe check if the image file is actually corrupted in Drive? good luck!

Have you considered using the Slides API directly? It’s a bit more complex to set up, but it offers better control and reliability for inserting images. Here’s a basic outline:

  1. Enable the Slides API in your Google Cloud project.
  2. Use the slides.presentations.batchUpdate method.
  3. Specify the image source as a Google Drive file in the request body.

This approach bypasses the need to download the image, which could be causing your issue. It’s also more efficient for larger files. You’ll need to modify your authorization scope to include the Slides API, but it’s worth the effort for smoother image insertions.

If you’re not comfortable with API calls, Pete_Magic’s suggestion is a solid alternative. Just ensure your script has the necessary Drive permissions.