1
- import { Jimp , cssColorToHex , rgbaToInt , JimpInstance } from "jimp" ;
1
+ import JimpModule = require ( "jimp" ) ;
2
2
import * as Color from "color" ;
3
3
import { exported } from "../../common/decorators" ;
4
4
import { AssetConstants } from "../../constants" ;
@@ -113,7 +113,7 @@ export class AssetsGenerationService implements IAssetsGenerationService {
113
113
assetItem . data ?. default ??
114
114
"white" ;
115
115
116
- const colorHEX : number = cssColorToHex ( color ) ;
116
+ const colorHEX : number = JimpModule . cssColorToHex ( color ) ;
117
117
const hex = colorHEX ?. toString ( 16 ) . substring ( 0 , 6 ) ?? "FFFFFF" ;
118
118
119
119
this . $fs . writeFile (
@@ -164,7 +164,8 @@ export class AssetsGenerationService implements IAssetsGenerationService {
164
164
continue ;
165
165
}
166
166
167
- let image : JimpInstance ;
167
+ let image : JimpModule . JimpInstance ;
168
+
168
169
switch ( operation ) {
169
170
case Operations . OverlayWith :
170
171
const overlayImageScale =
@@ -178,16 +179,10 @@ export class AssetsGenerationService implements IAssetsGenerationService {
178
179
imageResize ,
179
180
imageResize ,
180
181
) ;
181
- image = this . generateImage (
182
- background ,
183
- width ,
184
- height ,
185
- outputPath ,
186
- image ,
187
- ) ;
182
+ image = this . generateImage ( background , width , height , image ) ;
188
183
break ;
189
184
case Operations . Blank :
190
- image = this . generateImage ( background , width , height , outputPath ) ;
185
+ image = this . generateImage ( background , width , height ) ;
191
186
break ;
192
187
case Operations . Resize :
193
188
image = await this . resize ( generationData . imagePath , width , height ) ;
@@ -200,60 +195,56 @@ export class AssetsGenerationService implements IAssetsGenerationService {
200
195
assetItem . height ,
201
196
) ;
202
197
// The scale will apply to the underlying layer of the generated image
203
- image = this . generateImage (
204
- "#00000000" ,
205
- width ,
206
- height ,
207
- outputPath ,
208
- image ,
209
- ) ;
198
+ image = this . generateImage ( "#00000000" , width , height , image ) ;
210
199
break ;
211
200
default :
212
201
throw new Error ( `Invalid image generation operation: ${ operation } ` ) ;
213
202
}
214
203
215
204
// This code disables the alpha chanel, as some images for the Apple App Store must not have transparency.
216
205
if ( assetItem . rgba === false ) {
217
- //
218
- // The original code here became broken at some time and there is an issue posted here..
219
- // https://github.com/oliver-moran/jimp/issues/954
220
- // But NathanaelA recommended the below change and it works so maybe that's just what we go with.
221
- //
222
- // image = image.rgba(false);
223
- image = ( image as any ) . colorType ( 2 ) ;
206
+ // Add an underlying white layer
207
+ image = this . generateImage ( "#FFFFFF" , image . width , image . height , image ) ;
224
208
}
225
209
226
- image . write ( outputPath as any ) ;
210
+ if ( this . isAssetFilePath ( outputPath ) ) {
211
+ image . write ( outputPath ) ;
212
+ } else {
213
+ this . $logger . warn (
214
+ `Incorrect destination path ${ outputPath } for image ${ assetItem . filename } ` ,
215
+ ) ;
216
+ }
227
217
}
228
218
}
229
219
230
220
private async resize (
231
221
imagePath : string ,
232
222
width : number ,
233
223
height : number ,
234
- ) : Promise < JimpInstance > {
235
- const image = await Jimp . read ( imagePath ) ;
224
+ ) : Promise < JimpModule . JimpInstance > {
225
+ const image = await JimpModule . Jimp . read ( imagePath ) ;
236
226
return image . scaleToFit ( {
237
227
w : width ,
238
228
h : height ,
239
- } ) as any ;
229
+ } ) as JimpModule . JimpInstance ;
240
230
}
241
231
242
232
private generateImage (
243
233
background : string ,
244
234
width : number ,
245
235
height : number ,
246
- outputPath : string ,
247
- overlayImage ?: JimpInstance ,
248
- ) : JimpInstance {
249
- // Typescript declarations for Jimp are not updated to define the constructor with backgroundColor so we workaround it by casting it to <any> for this case only.
250
- const J = < any > Jimp ;
236
+ overlayImage ?: JimpModule . JimpInstance ,
237
+ ) : JimpModule . JimpInstance {
251
238
const backgroundColor = this . getRgbaNumber ( background ) ;
252
- let image = new J ( width , height , backgroundColor ) ;
239
+ let image = new JimpModule . Jimp ( {
240
+ width,
241
+ height,
242
+ color : backgroundColor ,
243
+ } ) ;
253
244
254
245
if ( overlayImage ) {
255
- const centeredWidth = ( width - overlayImage . bitmap . width ) / 2 ;
256
- const centeredHeight = ( height - overlayImage . bitmap . height ) / 2 ;
246
+ const centeredWidth = ( width - overlayImage . width ) / 2 ;
247
+ const centeredHeight = ( height - overlayImage . height ) / 2 ;
257
248
image = image . composite ( overlayImage , centeredWidth , centeredHeight ) ;
258
249
}
259
250
@@ -265,7 +256,21 @@ export class AssetsGenerationService implements IAssetsGenerationService {
265
256
const colorRgb = color . rgb ( ) ;
266
257
const alpha = Math . round ( colorRgb . alpha ( ) * 255 ) ;
267
258
268
- return rgbaToInt ( colorRgb . red ( ) , colorRgb . green ( ) , colorRgb . blue ( ) , alpha ) ;
259
+ return JimpModule . rgbaToInt (
260
+ colorRgb . red ( ) ,
261
+ colorRgb . green ( ) ,
262
+ colorRgb . blue ( ) ,
263
+ alpha ,
264
+ ) ;
265
+ }
266
+
267
+ private isAssetFilePath ( path : string ) : path is `${string } .${string } ` {
268
+ if ( ! path ) {
269
+ return false ;
270
+ }
271
+
272
+ const index = path . lastIndexOf ( "." ) ;
273
+ return index > - 1 && index < path . length - 1 ;
269
274
}
270
275
}
271
276
0 commit comments