Convert Google Slides Presentation to Image Sequence

C
Published in: JavaScript - Google Cloud

The Creator Studio add-on for Google Slides can export your Google Slides presentation to a variety of formats including animated GIF images, MP4 video and a sequence of images in PNG format.

Internally, the Slides add-on uses the Google API for Node.js to generate the PNG thumbnails of the presentation and then downloads them using the native Fetch API of the browser.

/* global gapi */

const SIZE = {
  UNSPECIFIED: 'THUMBNAIL_SIZE_UNSPECIFIED',
  LARGE: 'LARGE',
  MEDIUM: 'MEDIUM',
  SMALL: 'SMALL',
};

const IMAGE_SIZE = SIZE.SMALL;
const MAX_SLIDE_COUNT = 3;

const getSlideObjects = presentationId => {
  return new Promise((resolve, reject) => {
    gapi.client.slides.presentations
      .get({
        presentationId,
        fields: 'slides/objectId',
      })
      .then(({ result }) => {
        const pageObjects = result.slides.map(({ objectId }) => objectId);
        resolve(pageObjects.slice(0, MAX_SLIDE_COUNT));
      })
      .catch(err => reject(err));
  });
};

const getThumbnailUrl = (presentationId, pageObjectId) => {
  return new Promise((resolve, reject) => {
    gapi.client.slides.presentations.pages
      .getThumbnail({
        presentationId,
        pageObjectId,
        'thumbnailProperties.mimeType': 'PNG',
        'thumbnailProperties.thumbnailSize': IMAGE_SIZE,
      })
      .then(({ result }) => {
        resolve(result.contentUrl);
      })
      .catch(err => {
        reject(err);
      });
  });
};

const getImageLinks = presentationId => {
  return new Promise((resolve, reject) => {
    getSlideObjects(presentationId)
      .then(pageObjects => {
        return pageObjects.map(pageObjectId => {
          return getThumbnailUrl(presentationId, pageObjectId);
        });
      })
      .then(thumbnailUrls => {
        return Promise.all(thumbnailUrls);
      })
      .then(fileUrls => resolve(fileUrls.filter(url => url)))
      .catch(err => reject(err));
  });
};

export default getImageLinks;

The presentations.pages.getThumbnail method generates a PNG thumbnail image of the specified slide in the Google Presentation and returns a public URL of the thumbnail image.

Please note that getThumbnail is an ‘expensive’ operation and your Google project can only make 100 requests per 100 seconds per user. It is therefore a good idea to cache the results in localStore to avoid hitting the rate limits.

Published in: JavaScript - Google Cloud

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch