Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. /**
  2. * @fileoverview Resizes all images in a document to a desired width
  3. * while limited to a max size. Sizes are in cm.
  4. *
  5. * This code is meant to be implemented as a script on a Google Docs
  6. * document: Tools > Script editor.
  7. *
  8. * Once open, paste the code, adjust your desired sizes and run:
  9. * `resizeToDesiredWidth`.
  10. *
  11. * Justification for the inconsistent and outdated syntax:
  12. * The current version of Apps Script does not fully support ES6.
  13. * As a result, this code is a mix of both. For example, `const`
  14. * are not allowed at global level, but they are within blocks
  15. * or functions. Similarly, functions declared as:
  16. * `function name() {}` are detected to be run by the script, but
  17. * `const name = function() {}` would not. Arrow functions or
  18. * nice loop logic are not implemented.
  19. */
  20.  
  21. /** Configure the desired width and max height of the images. */
  22. var DESIRED_WIDTH_CM = 15;
  23. var MAX_HEIGHT_CM = 8;
  24.  
  25. /** This PPI is based on my experience. It may differ for you. */
  26. var PIXELS_PER_CM = 39;
  27.  
  28.  
  29. /**
  30. * Resizes all images in the document based on desired and max size.
  31. */
  32. function resizeToDesiredWidth() {
  33. const document = DocumentApp.getActiveDocument();
  34. const body = document.getBody();
  35. const images = body.getImages();
  36.  
  37. images.forEach(function(image) {
  38. const w = pxToCm(image.getWidth());
  39. const h = pxToCm(image.getHeight());
  40.  
  41. const newDimensions = getDesiredImageSize(w, h);
  42. const newW = newDimensions[0];
  43. const newH = newDimensions[1];
  44.  
  45. image.setWidth(cmToPx(newW));
  46. image.setHeight(cmToPx(newH));
  47. });
  48. }
  49.  
  50. /**
  51. * Calculates new sizes of images adjusted to desired and max.
  52. * Image ratio is preserved.
  53. * Resize calculation is done to DESIRED_WIDTH_CM.
  54. * If image is over MAX_HEIGHT_CM, then image is resized to MAX_HEIGHT_CM.
  55. * @param {number} imageW Image width in cm.
  56. * @param {number} imageH Image height in cm.
  57. * @return {!Array<number>} New image dimensions: [newImageW, newImageH]
  58. */
  59. var getDesiredImageSize = function(imageW, imageH) {
  60. const ratioW = imageW / DESIRED_WIDTH_CM;
  61. var newW = DESIRED_WIDTH_CM;
  62. var newH = imageH / ratioW;
  63.  
  64. if (newH <= MAX_HEIGHT_CM) {
  65. return [newW, newH];
  66. }
  67.  
  68. const ratioH = newH / MAX_HEIGHT_CM;
  69. newW = newW / ratioH;
  70. newH = MAX_HEIGHT_CM;
  71. return [newW, newH];
  72. };
  73.  
  74. /**
  75. * Converts a measure in pixels to cm.
  76. * @param {number} measureInPx
  77. * @return {number} Equivalent measure in cm.
  78. */
  79. var pxToCm = function(measureInPx) {
  80. return measureInPx / PIXELS_PER_CM;
  81. };
  82.  
  83. /**
  84. * Converts a measure in cm to pixels.
  85. * @param {number} measureInCm
  86. * @return {number} Equivalent measure in pixels.
  87. */
  88. var cmToPx = function(measureInCm) {
  89. return measureInCm * PIXELS_PER_CM;
  90. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement