Yes, it's possible to resize and compress images before appending them to the PDF document. This can help reduce the overall size of the PDF and make it more suitable for email attachments. You can use the getBlob() method to get the image blob and then resize it using the setWidth() and setHeight() methods while maintaining the aspect ratio.
Here's an updated version of your function with image resizing and compression:
javascriptfunction appendPhotos(body, allPhotos) { try { for (var i = 0; i < allPhotos.length; i++) { if (allPhotos[i] !== "") { var imageId = getIdFromUrl(allPhotos[i]); var imageFile = DriveApp.getFileById(imageId); var imageBlob = imageFile.getBlob(); // Get image dimensions var image = UrlFetchApp.fetch(allPhotos[i]); var blob = image.getBlob(); var img = blob.getAs('image/jpeg'); var width = img.getWidth(); var height = img.getHeight(); // Resize image if necessary var MAX_WIDTH = 640; var MAX_HEIGHT = 480; var ratio = width / height; var newWidth = width; var newHeight = height; if (width > MAX_WIDTH || height > MAX_HEIGHT) { if (ratio > 1) { newWidth = MAX_WIDTH; newHeight = MAX_WIDTH / ratio; } else { newHeight = MAX_HEIGHT; newWidth = MAX_HEIGHT * ratio; } } // Resize image var resizedImage = imageBlob.getScaledBlob(newWidth, newHeight); // Append resized image to the body body.appendImage(resizedImage); } } } catch (err) {
Logger.log("Error during appendPhotos"); Logger.log(err); } }
This code resizes the image to fit within a maximum width and height (640x480 by default) while maintaining the aspect ratio. Additionally, the image is converted to JPEG format to further reduce its size. This should help in reducing the overall size of the PDF attachment. You may adjust the MAX_WIDTH and MAX_HEIGHT constants according to your requirements.
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f0424ffd-ceee-40e4-94c9-f60f31a9beadn%40googlegroups.com.