11import { XmlElement } from '../types/xml-types' ;
22import { ImageStyle } from '../types/modify-types' ;
33import slugify from 'slugify' ;
4- import { imageSize } from 'image-size' ;
4+ import { imageSize } from 'image-size' ;
55import fs from 'fs' ;
66import { IPresentationProps } from '../interfaces/ipresentation-props' ;
7- import { computeSrcRectForNewImage , inferContainerAr } from './compute-src-rect' ;
7+ import {
8+ computeSrcRectForNewImage ,
9+ inferContainerAr ,
10+ } from './compute-src-rect' ;
811
912export default class ModifyImageHelper {
1013 /**
@@ -27,29 +30,29 @@ export default class ModifyImageHelper {
2730 * the presentation media folder using .loadMedia()
2831 * @param filename name of target image in root template media folder.
2932 * @param pres presentation properties (to access the root template archive)
30- * @param newImageWidth width of the new image
31- * @param newImageHeight height of the new image
3233 */
3334 static setRelationTargetCover = (
3435 filename : string ,
3536 pres : IPresentationProps ,
3637 ) => {
3738 return async ( element : XmlElement , arg1 : XmlElement ) : Promise < void > => {
38-
3939 const newTarget = '../media/' + slugify ( filename ) ;
4040 const originalTarget = arg1 . getAttribute ( 'Target' ) ;
4141 const originalTargetPath = originalTarget . replace ( '../' , 'ppt/' ) ;
4242 const originalImageDimensions = { width : 100 , height : 100 } ;
4343 const newImageDimensions = { width : 100 , height : 100 } ;
4444
45-
4645 // Get the new image dimensions, using the rootTemplate mediafiles array,
4746 // since we have it loaded into the presentation media folder using .loadMedia()
4847 // If we don't find the media file, we warn, but continue
4948 try {
50- const mediaFile = pres . rootTemplate . mediaFiles . find ( file => file . file === filename ) ;
51- if ( ! mediaFile ) {
52- throw new Error ( "Media file not found in template archive in path: " + filename ) ;
49+ const mediaFile = pres . rootTemplate . mediaFiles . find (
50+ ( file ) => file . file === filename ,
51+ ) ;
52+ if ( ! mediaFile ) {
53+ throw new Error (
54+ 'Media file not found in template archive in path: ' + filename ,
55+ ) ;
5356 }
5457 const buffer = fs . readFileSync ( mediaFile . filepath ) ;
5558 const _dimensions = imageSize ( buffer ) ;
@@ -58,53 +61,70 @@ export default class ModifyImageHelper {
5861 } catch ( error ) {
5962 console . warn ( "Couldn't find media file in template archive in path." ) ;
6063 }
61-
64+
6265 // Find the original image dimensions using the original target path from the original slide
6366 // using the rootTemplate archive file system and get the image dimensions.
6467 // If we don't find the original image, we warn, but continue
6568 // If we find the original image, we get the image dimensions and use this to reverse calculate
6669 // the aspect ratio and then use the new image dimensions to calculate and set the new crop on srcRect.
6770 // This results in the image being cropped in the image container to match the aspect ratio of the new image.
6871 try {
69- if ( pres . rootTemplate . archive . fileExists ( originalTargetPath ) ) {
70- const originalImage = await pres . rootTemplate . archive . read ( originalTargetPath , "nodebuffer" ) ;
72+ if ( pres . rootTemplate . archive . fileExists ( originalTargetPath ) ) {
73+ const originalImage = await pres . rootTemplate . archive . read (
74+ originalTargetPath ,
75+ 'nodebuffer' ,
76+ ) ;
7177 const _dimensions = imageSize ( originalImage ) ;
7278 originalImageDimensions . width = _dimensions . width ;
7379 originalImageDimensions . height = _dimensions . height ;
7480 } else {
75- throw new Error ( "Original image not found from template archive in path: " + originalTargetPath ) ;
81+ throw new Error (
82+ 'Original image not found from template archive in path: ' +
83+ originalTargetPath ,
84+ ) ;
7685 }
77-
86+
7887 const srcRect = element . getElementsByTagName ( 'a:srcRect' ) [ 0 ] ;
7988 const srcRectLeft = srcRect . getAttribute ( 'l' ) ;
8089 const srcRectTop = srcRect . getAttribute ( 't' ) ;
8190 const srcRectRight = srcRect . getAttribute ( 'r' ) ;
8291 const srcRectBottom = srcRect . getAttribute ( 'b' ) ;
83-
92+
8493 const currentSrcRect = {
8594 l : srcRectLeft ? Number ( srcRectLeft ) : 0 ,
8695 t : srcRectTop ? Number ( srcRectTop ) : 0 ,
8796 r : srcRectRight ? Number ( srcRectRight ) : 0 ,
8897 b : srcRectBottom ? Number ( srcRectBottom ) : 0 ,
89- }
90-
91- const containerAr = inferContainerAr ( originalImageDimensions . width , originalImageDimensions . height , currentSrcRect ) ;
92-
93- const newSrcRect = computeSrcRectForNewImage ( containerAr , newImageDimensions . width , newImageDimensions . height ) ;
94-
98+ } ;
99+
100+ const containerAr = inferContainerAr (
101+ originalImageDimensions . width ,
102+ originalImageDimensions . height ,
103+ currentSrcRect ,
104+ ) ;
105+
106+ const newSrcRect = computeSrcRectForNewImage (
107+ containerAr ,
108+ newImageDimensions . width ,
109+ newImageDimensions . height ,
110+ ) ;
111+
95112 srcRect . setAttribute ( 'l' , String ( newSrcRect . l ) ) ;
96113 srcRect . setAttribute ( 't' , String ( newSrcRect . t ) ) ;
97114 srcRect . setAttribute ( 'r' , String ( newSrcRect . r ) ) ;
98115 srcRect . setAttribute ( 'b' , String ( newSrcRect . b ) ) ;
99116 } catch ( error ) {
100- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
101- console . warn ( "Skipped setting relation target cropped due to an error: " + errorMessage ) ;
117+ const errorMessage =
118+ error instanceof Error ? error . message : String ( error ) ;
119+ console . warn (
120+ 'Skipped setting relation target cropped due to an error: ' +
121+ errorMessage ,
122+ ) ;
102123 }
103-
104- arg1 . setAttribute ( 'Target' , newTarget ) ;
105124
125+ arg1 . setAttribute ( 'Target' , newTarget ) ;
106126 } ;
107- }
127+ } ;
108128
109129 /*
110130 Update an existing duotone image overlay element (WIP)
0 commit comments