@@ -5,19 +5,19 @@ const Jimp = require("jimp");
55const fs = require ( "fs" ) ;
66const path = require ( "path" ) ;
77const worker_threads_1 = require ( "worker_threads" ) ;
8- const outputFolder = " output" ;
8+ const outputFolder = ' output' ;
99/**
1010 * Function to slice an image into smaller segments
1111 */
12- function sliceImage ( filename , width , height , skipExtCheck ) {
12+ function sliceImage ( filename , width , height , canvasWidth , canvasHeight , skipExtCheck ) {
1313 Jimp . read ( filename , ( err , image ) => {
1414 if ( err && skipExtCheck ) {
1515 console . error ( err ) ;
1616 }
1717 else {
1818 // Continue slicing if image is successfully read
1919 if ( image ) {
20- continueSlicing ( image , width , height , filename ) ;
20+ continueSlicing ( image , width , height , canvasWidth , canvasHeight , filename ) ;
2121 return ;
2222 }
2323 }
@@ -26,7 +26,7 @@ function sliceImage(filename, width, height, skipExtCheck) {
2626 return ;
2727 }
2828 // Check for supported image formats if skipExtCheck is false
29- const supportedFormats = [ " .png" , " .gif" , " .jpg" , " .jpeg" ] ;
29+ const supportedFormats = [ ' .png' , ' .gif' , ' .jpg' , ' .jpeg' ] ;
3030 let foundImage = false ;
3131 // Attempt to read the image with different extensions
3232 supportedFormats . forEach ( ( ext ) => {
@@ -35,7 +35,7 @@ function sliceImage(filename, width, height, skipExtCheck) {
3535 Jimp . read ( fullFilename , ( err , image ) => {
3636 if ( ! foundImage && ! err ) {
3737 foundImage = true ;
38- continueSlicing ( image , width , height , fullFilename ) ;
38+ continueSlicing ( image , width , height , canvasWidth , canvasHeight , fullFilename ) ;
3939 }
4040 } ) ;
4141 }
@@ -45,7 +45,7 @@ exports.sliceImage = sliceImage;
4545/**
4646 * Continue slicing the image into smaller segments
4747 */
48- function continueSlicing ( image , width , height , inputFilename ) {
48+ function continueSlicing ( image , width , height , canvasWidth , canvasHeight , inputFilename ) {
4949 // If height is not specified, use width as height
5050 height = height || width ;
5151 const imageWidth = image . getWidth ( ) ;
@@ -68,7 +68,21 @@ function continueSlicing(image, width, height, inputFilename) {
6868 // Incorporate the input filename into the output filename
6969 const baseFilename = path . basename ( inputFilename , path . extname ( inputFilename ) ) ;
7070 const outputFilename = `${ outputFolder } /${ baseFilename } _${ x } _${ y } .png` ;
71- slice . write ( outputFilename ) ;
71+ if ( canvasWidth || canvasHeight ) {
72+ // Calculate canvas dimensions
73+ const finalCanvasWidth = canvasWidth || width ;
74+ const finalCanvasHeight = ( canvasHeight || canvasWidth ) ?? height ;
75+ // Create a new canvas with transparent background
76+ const canvas = new Jimp ( finalCanvasWidth , finalCanvasHeight , 0x00000000 ) ;
77+ // Composite the image in the middle of the canvas
78+ const startX2 = Math . floor ( ( finalCanvasWidth - sliceWidth ) / 2 ) ;
79+ const startY2 = Math . floor ( ( finalCanvasHeight - sliceHeight ) / 2 ) ;
80+ canvas . composite ( slice , startX2 , startY2 ) ;
81+ canvas . write ( outputFilename ) ;
82+ }
83+ else {
84+ slice . write ( outputFilename ) ;
85+ }
7286 console . log ( `Slice saved: ${ outputFilename } ` ) ;
7387 }
7488 }
@@ -77,6 +91,6 @@ function continueSlicing(image, width, height, inputFilename) {
7791if ( ! worker_threads_1 . isMainThread ) {
7892 const { filePath, options } = worker_threads_1 . workerData ;
7993 options . filename = filePath ;
80- const { filename, width, height } = options ;
81- sliceImage ( filename , width , height , true ) ;
94+ const { filename, width, height, canvasWidth , canvasHeight } = options ;
95+ sliceImage ( filename , width , height , canvasWidth , canvasHeight , true ) ;
8296}
0 commit comments