79344779

Date: 2025-01-10 06:26:31
Score: 1.5
Natty:
Report link

After exploring the issue further, I found a working solution that builds on the suggestion provided by Alma_Matters. Thank you for pointing me in the right direction!

Here’s the updated and improved code:

/**
 * Adds a logo to an image.
 * The logo size is set to 20% of the smaller dimension of the base image.
 * The page size is adjusted to match the image dimensions to avoid empty margins.
 * 
 * @param {Blob} imageBlob - The base image.
 * @param {Blob} logoBlob - The logo image.
 * @return {{width: number, height: number}} A merged image blob with the logo.
 */
function addLogoToImage(imageBlob, logoBlob) {
  // Get the image size
  const size = getSize(imageBlob),
    width = size.width,
    height = size.height;

  // Create a slide with the same dimensions as the image
  const slides = DocsServiceApp.createNewSlidesWithPageSize({
    title: 'temp',
    parent: Drive.Files.get("root").id,
    width: { unit: "pixel", size: width },
    height: { unit: "pixel", size: height }
  });

  // Calculate the logo size (20% of the smaller dimension)
  const minimumSize = Math.min(width, height) * 0.2;

  // Access the slide
  const presentation = SlidesApp.openById(slides);
  const slide = presentation.getSlides()[0];

  // Add the base image
  slide.insertImage(imageBlob);

  // Add the logo
  slide.insertImage(logoBlob, 10, 10, minimumSize, minimumSize);

  // Save and close the presentation
  presentation.saveAndClose();

  // Export the slide as an image
  const response = UrlFetchApp.fetch(`https://docs.google.com/presentation/d/${slides}/export/png`, {
    headers: {
      Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
    },
  });

  // Retrieve the merged image as a blob
  const mergedImage = response.getBlob();

  // Delete the temporary presentation
  Drive.Files.remove(slides);

  return mergedImage;
}

/**
 * Retrieves the dimensions of an image blob.
 * Based on code from ImgApp (https://github.com/tanaikech/ImgApp).
 * Copyright (c) 2018 Tanaike.
 * Licensed under the MIT License (https://opensource.org/licenses/MIT).
 * 
 * @param {Blob} blob - The image blob to analyze.
 * @return {{width: number, height: number}} An object containing the width and height of the image.
 */
function getSize(blob) {
  const docfile = Drive.Files.create({
    title: "size",
    mimeType: "application/vnd.google-apps.document",
  }).id;
  const img = DocumentApp.openById(docfile).insertImage(0, blob);
  Drive.Files.remove(docfile);
  return { width: img.getWidth(), height: img.getHeight() };
}

Explanation:

For this code to work properly, the DocsServiceApp library, as well as Slides and Drive services must be installed in AppsScript.

This updated code resolves the issue while ensuring the output image is clean and properly formatted.

Example of using the code:

function test() {
  var imageBlob = UrlFetchApp.fetch("https://......jpg").getBlob();
  var logoBlob = UrlFetchApp.fetch("https://.......png").getBlob();
  const mergedImage = addLogoToImage(imageBlob, logoBlob);
  DriveApp.createFile(mergedImage);
}

Disclosure: I created this code and wrote this answer myself. Since I'm not fluent in English, I used GPT to help me refine the language and ensure clarity. The ChatGPT did not touch on the content of the code itself, only the translation of the answer into English.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: הלוי הלוי