@@ -5,42 +5,54 @@ const Jimp = require("jimp");
55const fs = require ( "fs" ) ;
66const path = require ( "path" ) ;
77const worker_threads_1 = require ( "worker_threads" ) ;
8- const outputFolder = 'output' ;
8+ function errorCallback ( err ) {
9+ if ( err ) {
10+ console . error ( err ) ;
11+ }
12+ }
913/**
1014 * Function to slice an image into smaller segments
1115 */
1216function sliceImage ( filename , width , height , canvasWidth , canvasHeight , scale , cubic , skipExtCheck ) {
13- Jimp . read ( filename , ( err , image ) => {
14- if ( err && skipExtCheck ) {
17+ Jimp . read ( filename )
18+ . then ( ( image ) => {
19+ // Continue slicing if image is successfully read
20+ if ( image ) {
21+ skipExtCheck = true ;
22+ continueSlicing ( image , width , height , canvasWidth , canvasHeight , scale , cubic , filename ) ;
23+ }
24+ } )
25+ . catch ( ( err ) => {
26+ if ( skipExtCheck ) {
1527 console . error ( err ) ;
1628 }
17- else {
18- // Continue slicing if image is successfully read
19- if ( image ) {
20- continueSlicing ( image , width , height , canvasWidth , canvasHeight , scale , cubic , filename ) ;
21- }
29+ } )
30+ . finally ( ( ) => {
31+ if ( skipExtCheck ) {
32+ return ;
2233 }
23- } ) ;
24- if ( skipExtCheck ) {
25- return ;
26- }
27- // Check for supported image formats if skipExtCheck is false
28- const supportedFormats = [ '.png' , '.gif' , '.jpg' , '.jpeg' ] ;
29- let foundImage = false ;
30- // Attempt to read the image with different extensions
31- supportedFormats . forEach ( async ( ext , index ) => {
32- const fullFilename = filename + ext ;
33- if ( ! foundImage ) {
34- try {
35- const image = await Jimp . read ( fullFilename ) ;
34+ // Check for supported image formats if skipExtCheck is false
35+ const supportedFormats = [ '.png' , '.gif' , '.jpg' , '.jpeg' ] ;
36+ let foundImage = false ;
37+ // Attempt to read the image with different extensions
38+ const promises = supportedFormats . map ( ( ext ) => {
39+ const fullFilename = filename + ext ;
40+ return ( Jimp . read ( fullFilename )
41+ . then ( ( image ) => {
3642 foundImage = true ;
3743 continueSlicing ( image , width , height , canvasWidth , canvasHeight , scale , cubic , fullFilename ) ;
44+ } )
45+ // Silence errors since we'll handle them later
46+ . catch ( ( ) => { } ) ) ;
47+ } ) ;
48+ // Wait for all promises to be resolved
49+ Promise . all ( promises )
50+ . then ( ( ) => {
51+ if ( ! foundImage ) {
52+ console . error ( `Error: Could not find ${ filename } ` ) ;
3853 }
39- catch ( err ) { }
40- if ( ! foundImage && index === supportedFormats . length - 1 ) {
41- console . error ( `Could not find ${ filename } ` ) ;
42- }
43- }
54+ } )
55+ . catch ( errorCallback ) ;
4456 } ) ;
4557}
4658exports . sliceImage = sliceImage ;
@@ -57,6 +69,7 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale,
5769 const horizontalSlices = Math . ceil ( imageWidth / width ) ;
5870 const verticalSlices = Math . ceil ( imageHeight / height ) ;
5971 // Create a folder for output if it doesn't exist
72+ const outputFolder = 'output' ;
6073 if ( ! fs . existsSync ( outputFolder ) ) {
6174 fs . mkdirSync ( outputFolder ) ;
6275 }
@@ -84,13 +97,13 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale,
8497 if ( scale !== 1 ) {
8598 canvas . scale ( scale , cubic ? Jimp . RESIZE_BICUBIC : Jimp . RESIZE_NEAREST_NEIGHBOR ) ;
8699 }
87- canvas . write ( outputFilename ) ;
100+ canvas . write ( outputFilename , errorCallback ) ;
88101 }
89102 else {
90103 if ( scale !== 1 ) {
91104 slice . scale ( scale , cubic ? Jimp . RESIZE_BICUBIC : Jimp . RESIZE_NEAREST_NEIGHBOR ) ;
92105 }
93- slice . write ( outputFilename ) ;
106+ slice . write ( outputFilename , errorCallback ) ;
94107 }
95108 console . log ( `Slice saved: ${ outputFilename } ` ) ;
96109 }
0 commit comments