@@ -299,10 +299,34 @@ const extractFeatures = async page => {
299299 }
300300}
301301
302+ let sharp = null
303+ const resizeCanvas = async ( image , resX , resY ) => {
304+ if ( ! sharp ) sharp = require ( "sharp" )
305+ const sharpImage = sharp ( image )
306+
307+ /**
308+ * TODO: we should eventually get the canvas width/height from the page context
309+ * when running captureCanvas() - can bypass sharp if the image is small enough
310+ */
311+ // get current image dimensions to check if resize is needed
312+ const metadata = await sharpImage . metadata ( )
313+ const currentWidth = metadata . width
314+ const currentHeight = metadata . height
315+
316+ // check if current resolution is already <= target resolution
317+ if ( currentWidth <= resX && currentHeight <= resY ) {
318+ // no resize needed, return original image
319+ return image
320+ }
321+
322+ return sharpImage . resize ( resX , resY , { fit : "inside" } ) . toBuffer ( )
323+ }
302324const performCapture = async (
303325 mode ,
304326 page ,
305327 canvasSelector ,
328+ resX ,
329+ resY ,
306330 gif ,
307331 frameCount ,
308332 captureInterval ,
@@ -318,14 +342,16 @@ const performCapture = async (
318342 // if the mode is canvas, we need to execute som JS on the client to select
319343 // the canvas and generate a dataURL to bridge it in here
320344 else if ( mode === "CANVAS" ) {
321- return captureCanvas (
345+ const canvas = await captureCanvas (
322346 page ,
323347 canvasSelector ,
324348 gif ,
325349 frameCount ,
326350 captureInterval ,
327351 playbackFps
328352 )
353+ if ( resX && resY ) return resizeCanvas ( canvas , resX , resY )
354+ return canvas
329355 }
330356}
331357
@@ -647,6 +673,8 @@ exports.handler = async (event, context) => {
647673 mode ,
648674 page ,
649675 canvasSelector ,
676+ resX ,
677+ resY ,
650678 gif ,
651679 frameCount ,
652680 captureInterval ,
0 commit comments