メインコンテンツにスキップ

ImgApp

This is a library of image tools for Google Apps Script.

AI Summary

ImgApp

Utility library for Google Apps Script that enables image size retrieval, resizing, thumbnail updating, and crop/merge operations.

Target Users

Intermediate-to-advanced GAS developers building internal tools who need to automate image handling without complex Drive/Slides API coding.

Problems Solved

GAS lacks native methods for getting image dimensions, resizing, or editing images; workarounds via Docs are slow and cumbersome.

Tags

Main Features

1
Fast image dimension retrieval

getSize() parses BMP/GIF/PNG/JPG binaries directly, returning width and height in milliseconds without using DocumentApp.

2
Resize various Drive files

doResize() leverages Drive thumbnail endpoints to resize images, videos, Google Docs, PDFs and more to a given width and returns a Blob.

3
Update Drive file thumbnails

updateThumbnail() lets users replace or add thumbnails (e.g., for zip files) with PNG/JPEG/GIF images via multipart upload.

4
Crop and merge images

editImage() combines Slides API to crop images or merge multiple images on a slide sized to output and returns the result as a Blob.

Usage Examples

Retrieve image dimensions quickly

function sampleGetSize() {
  const fileId = "<IMAGE_FILE_ID_FROM_DRIVE>"; // Drive image file ID
  const blob = DriveApp.getFileById(fileId).getBlob();
  // Get width & height with ImgApp
  const info = ImgApp.getSize(blob);
  Logger.log(`width:${info.width}px height:${info.height}px`);
}

This snippet fetches an image from Drive, passes its Blob to ImgApp.getSize(), and logs the pixel dimensions.

Resize a file and save back to Drive

function sampleResize() {
  const fileId = "<DRIVE_FILE_ID>"; // Can be image, PDF, Google Doc, etc.
  const targetWidth = 800;            // Desired width in pixels
  // Resize with ImgApp
  const result = ImgApp.doResize(fileId, targetWidth);
  // Log new size
  Logger.log(`resized size: ${result.resizedwidth}x${result.resizedheight}`);
  // Save resized Blob as a file
  DriveApp.createFile(result.blob.setName("resized.png"));
}

ImgApp.doResize() uses Drive's thumbnail endpoint to produce an image of the specified width and returns it as a Blob for immediate saving or further processing.