@@ -11,15 +11,17 @@ var config = require('../server-config.js');
1111var extend = require ( 'util' ) . _extend ;
1212var url = require ( 'url' ) ;
1313var mime = require ( 'mime-types' ) ;
14- var Jimp = require ( 'jimp' ) ;
14+ var { Jimp, loadFont, JimpMime, HorizontalAlign, VerticalAlign } = require ( 'jimp' ) ;
15+ var JimpFonts = require ( "jimp/fonts" ) ;
1516var font ;
16- Jimp . loadFont ( Jimp . FONT_SANS_32_BLACK , function ( err , f ) {
17- font = f ;
18- } ) ;
1917var font2x ;
20- Jimp . loadFont ( Jimp . FONT_SANS_64_BLACK , function ( err , f ) {
21- font2x = f ;
22- } ) ;
18+
19+ async function loadFonts ( ) {
20+ font = await loadFont ( JimpFonts . SANS_32_BLACK ) ;
21+ font2x = await loadFont ( JimpFonts . SANS_64_BLACK ) ;
22+ } ;
23+
24+ loadFonts ( ) ;
2325
2426app . use ( require ( 'connect-livereload' ) ( { ignore : [ / ^ \/ d l / , / ^ \/ i m g / , / ^ \/ u p l o a d / ] } ) ) ;
2527
@@ -98,7 +100,7 @@ app.use('/upload/', uploadHandler.array('files[]', 20), function(req, res) {
98100} ) ;
99101
100102// imgProcessorBackend + "?src=" + encodeURIComponent(src) + "&method=" + encodeURIComponent(method) + "¶ms=" + encodeURIComponent(width + "," + height);
101- app . get ( '/img/' , function ( req , res ) {
103+ app . get ( '/img/' , async function ( req , res ) {
102104
103105 var params = req . query . params . split ( ',' ) ;
104106
@@ -109,109 +111,98 @@ app.get('/img/', function(req, res) {
109111 var text = req . query . text ? req . query . text : '' + w + ' x ' + h ;
110112 var workScale = 1 ;
111113
112- new Jimp ( w * workScale , h * workScale , '#808080' , function ( err , image ) {
113- image . scan ( 0 , 0 , image . bitmap . width , image . bitmap . height , function ( x , y , idx ) {
114- if ( ( ( ( Math . ceil ( image . bitmap . height / ( size * workScale * 2 ) ) + 1 ) * ( size * workScale * 2 ) + x - y ) % ( size * workScale * 2 ) ) < size * workScale ) image . setPixelColor ( 0x707070FF , x , y ) ;
115- if ( x == image . bitmap . width - 1 && y == image . bitmap . height - 1 ) {
116- var tempImg = new Jimp ( w * workScale , h * workScale , 0x0 )
117- . print ( req . query . method == 'placeholder2' ? font2x : font , 0 , 0 , {
118- text : text ,
119- alignmentX : Jimp . HORIZONTAL_ALIGN_CENTER ,
120- alignmentY : Jimp . VERTICAL_ALIGN_MIDDLE
121- } , w * workScale , h * workScale )
122- . color ( [ { apply : 'xor' , params : [ '#B0B0B0' ] } ] , function ( err , tempImg2 ) {
123- if ( err ) {
124- console . log ( "Error #1 creating placeholder: " , err ) ;
125- res . status ( 500 ) . send ( 'Error #1 creating placeholder: ' + err . message ) ;
126- } else {
127- image . blit ( tempImg2 , 0 , 0 )
128- . getBuffer ( Jimp . MIME_PNG , function ( error , buffer ) {
129- if ( error ) {
130- console . log ( "Error #2 creating placeholder: " , error ) ;
131- res . status ( 500 ) . send ( 'Error #1 creating placeholder: ' + err . message ) ;
132- } else res . status ( 200 ) . contentType ( 'image/png' ) . send ( new Buffer ( buffer ) ) ;
133- } ) ;
134- }
135- } ) ;
136- }
137- } ) ;
114+ const image = new Jimp ( { width : w * workScale , height : h * workScale , color : '#808080' } ) ;
115+ image . scan ( 0 , 0 , image . bitmap . width , image . bitmap . height , function ( x , y , idx ) {
116+ if ( ( ( ( Math . ceil ( image . bitmap . height / ( size * workScale * 2 ) ) + 1 ) * ( size * workScale * 2 ) + x - y ) % ( size * workScale * 2 ) ) < size * workScale ) image . setPixelColor ( 0x707070FF , x , y ) ;
138117 } ) ;
139118
119+ var tempImg = new Jimp ( { width : w * workScale , height : h * workScale , color : 0x0 } )
120+ . print ( {
121+ font : req . query . method == 'placeholder2' ? font2x : font ,
122+ x : 0 ,
123+ y : 0 ,
124+ text : {
125+ text : text ,
126+ alignmentX : HorizontalAlign . CENTER , // .HORIZONTAL_ALIGN_CENTER,
127+ alignmentY : VerticalAlign . MIDDLE , // Jimp.VERTICAL_ALIGN_MIDDLE
128+ } ,
129+ maxWidth : w * workScale ,
130+ maxHeight : h * workScale
131+ } )
132+ . color ( [ { apply : 'xor' , params : [ /* '#B0B0B0' */ { r : 176 , g : 176 , b : 176 } ] } ] ) ;
133+ image . blit ( tempImg , 0 , 0 ) . getBuffer ( JimpMime . png ) . then ( buffer => {
134+ res . status ( 200 ) . contentType ( 'image/png' ) . send ( Buffer . from ( buffer ) ) ;
135+ } ) . catch ( err => {
136+ console . log ( "Error #2 creating placeholder: " , err ) ;
137+ res . status ( 500 ) . send ( 'Error #1 creating placeholder: ' + err . message ) ;
138+ } ) ;
139+
140+
140141 } else if ( req . query . method == 'resize' || req . query . method == 'resizex' || req . query . method == 'cover' || req . query . method == 'coverx' || req . query . method == 'aspect' ) {
141142 // NOTE: req.query.src is an URL: we do parse it to localpath.
142143 var urlparsed = url . parse ( req . query . src ) ;
143144 var src = "./" + decodeURI ( urlparsed . pathname ) ;
144145
145- var ir = Jimp . read ( src , function ( err , image ) {
146+ Jimp . read ( src ) . then ( image => {
146147
147- if ( err ) {
148- console . log ( "Error reading image: " , err . message ) ;
149- res . status ( 404 ) . send ( 'Not found' ) ;
150- } else {
151-
152- // "aspect" method is currently unused, but we're evaluating it.
153- if ( req . query . method == 'aspect' ) {
154- var oldparams = [ params [ 0 ] , params [ 1 ] ] ;
155- if ( params [ 0 ] / params [ 1 ] > image . bitmap . width / image . bitmap . height ) {
156- params [ 1 ] = Math . round ( image . bitmap . width / ( params [ 0 ] / params [ 1 ] ) ) ;
157- params [ 0 ] = image . bitmap . width ;
158- } else {
159- params [ 0 ] = Math . round ( image . bitmap . height * ( params [ 0 ] / params [ 1 ] ) ) ;
160- params [ 1 ] = image . bitmap . height ;
161- }
148+ // "aspect" method is currently unused, but we're evaluating it.
149+ if ( req . query . method == 'aspect' ) {
150+ var oldparams = [ params [ 0 ] , params [ 1 ] ] ;
151+ if ( params [ 0 ] / params [ 1 ] > image . bitmap . width / image . bitmap . height ) {
152+ params [ 1 ] = Math . round ( image . bitmap . width / ( params [ 0 ] / params [ 1 ] ) ) ;
153+ params [ 0 ] = image . bitmap . width ;
154+ } else {
155+ params [ 0 ] = Math . round ( image . bitmap . height * ( params [ 0 ] / params [ 1 ] ) ) ;
156+ params [ 1 ] = image . bitmap . height ;
162157 }
158+ }
163159
164- // res.set('Content-Type', 'image/'+format.toLowerCase());
165- var sendOrError = function ( err , image ) {
166- if ( err ) {
167- console . log ( "Error manipulating image: " , err ) ;
168- res . status ( 500 ) . send ( 'Unexpected condition: ' + err . message ) ;
169- } else {
170- image . getBuffer ( Jimp . MIME_PNG , function ( error , buffer ) {
171- if ( error ) {
172- console . log ( "Error sending manipulated image: " , error ) ;
173- res . status ( 500 ) . send ( 'Unexpected condition manipulating image: ' + error . message ) ;
174- } else res . status ( 200 ) . contentType ( 'image/png' ) . send ( new Buffer ( buffer ) ) ;
175- } ) ;
176- }
177- } ;
178- if ( req . query . method == 'resize' || req . query . method == 'resizex' ) {
179- if ( params [ 0 ] == 'null' ) {
180- if ( req . query . method == 'resize' || image . bitmap . height > parseInt ( params [ 1 ] ) ) image . resize ( Jimp . AUTO , parseInt ( params [ 1 ] ) , sendOrError ) ;
181- else sendOrError ( false , image ) ;
182- } else if ( params [ 1 ] == 'null' ) {
183- if ( req . query . method == 'resize' || image . bitmap . width > parseInt ( params [ 0 ] ) ) image . resize ( parseInt ( params [ 0 ] ) , Jimp . AUTO , sendOrError ) ;
184- else sendOrError ( false , image ) ;
185- } else {
186- if ( req . query . method == 'resize' || image . bitmap . width > parseInt ( params [ 0 ] ) || image . bitmap . height > parseInt ( params [ 1 ] ) ) image . contain ( parseInt ( params [ 0 ] ) , parseInt ( params [ 1 ] ) , sendOrError ) ;
187- else sendOrError ( false , image ) ;
188- }
160+ var sendImage = function ( image ) {
161+ image . getBuffer ( JimpMime . png ) . then ( buffer => {
162+ res . status ( 200 ) . contentType ( 'image/png' ) . send ( Buffer . from ( buffer ) ) ;
163+ } ) . catch ( error => {
164+ console . log ( "Error sending manipulated image: " , error ) ;
165+ res . status ( 500 ) . send ( 'Unexpected condition manipulating image: ' + error . message ) ;
166+ } ) ;
167+ } ;
168+ if ( req . query . method == 'resize' || req . query . method == 'resizex' ) {
169+ if ( params [ 0 ] == 'null' ) {
170+ if ( req . query . method == 'resize' || image . bitmap . height > parseInt ( params [ 1 ] ) ) sendImage ( image . resize ( { h : parseInt ( params [ 1 ] ) } ) ) ;
171+ else sendImage ( image ) ;
172+ } else if ( params [ 1 ] == 'null' ) {
173+ if ( req . query . method == 'resize' || image . bitmap . width > parseInt ( params [ 0 ] ) ) sendImage ( image . resize ( { w : parseInt ( params [ 0 ] ) } ) ) ;
174+ else sendImage ( image ) ;
189175 } else {
190- // Compute crop coordinates for cover algorythm
191- var w = parseInt ( params [ 0 ] ) ;
192- var h = parseInt ( params [ 1 ] ) ;
193- var ar = w / h ;
194- var origAr = image . bitmap . width / image . bitmap . height ;
195- if ( ar > origAr ) {
196- var newH = Math . round ( image . bitmap . width / ar ) ;
197- var newY = Math . round ( ( image . bitmap . height - newH ) / 2 ) ;
198- image . crop ( 0 , newY , image . bitmap . width , newH ) ;
199- // coverx does not enlarge cropped images
200- if ( req . query . method == 'cover' || newH > h ) image . resize ( w , h , sendOrError ) ;
201- else sendOrError ( false , image ) ;
202- } else {
203- var newW = Math . round ( image . bitmap . height * ar ) ;
204- var newX = Math . round ( ( image . bitmap . width - newW ) / 2 ) ;
205- image . crop ( newX , 0 , newW , image . bitmap . height ) ;
206- // coverx does not enlarge cropped images
207- if ( req . query . method == 'cover' || newW > w ) image . resize ( w , h , sendOrError ) ;
208- else sendOrError ( false , image ) ;
209- }
176+ if ( req . query . method == 'resize' || image . bitmap . width > parseInt ( params [ 0 ] ) || image . bitmap . height > parseInt ( params [ 1 ] ) ) sendImage ( image . contain ( { w : parseInt ( params [ 0 ] ) , h : parseInt ( params [ 1 ] ) } ) ) ;
177+ else sendImage ( image ) ;
178+ }
179+ } else {
180+ // Compute crop coordinates for cover algorythm
181+ var w = parseInt ( params [ 0 ] ) ;
182+ var h = parseInt ( params [ 1 ] ) ;
183+ var ar = w / h ;
184+ var origAr = image . bitmap . width / image . bitmap . height ;
185+ if ( ar > origAr ) {
186+ var newH = Math . round ( image . bitmap . width / ar ) ;
187+ var newY = Math . round ( ( image . bitmap . height - newH ) / 2 ) ;
188+ image . crop ( { x : 0 , y : newY , w : image . bitmap . width , h : newH } ) ;
189+ // coverx does not enlarge cropped images
190+ if ( req . query . method == 'cover' || newH > h ) sendImage ( image . resize ( { w : w , h : h } ) ) ;
191+ else sendImage ( image ) ;
192+ } else {
193+ var newW = Math . round ( image . bitmap . height * ar ) ;
194+ var newX = Math . round ( ( image . bitmap . width - newW ) / 2 ) ;
195+ image . crop ( { x : newX , y : 0 , w : newW , h : image . bitmap . height } ) ;
196+ // coverx does not enlarge cropped images
197+ if ( req . query . method == 'cover' || newW > w ) sendImage ( image . resize ( { w : w , h : h } ) ) ;
198+ else sendImage ( image ) ;
210199 }
211-
212200 }
213-
201+ } ) . catch ( err => {
202+ console . log ( "Error reading image: " , err ) ;
203+ res . status ( 404 ) . send ( 'Not found' ) ;
214204 } ) ;
205+
215206 }
216207
217208} ) ;
0 commit comments