1
1
const fs = require ( 'fs' )
2
2
const ora = require ( 'ora' )
3
- const { createCanvas , loadImage } = require ( 'canvas ' )
3
+ const sharp = require ( 'sharp ' )
4
4
const Json2csvParser = require ( 'json2csv' ) . Parser
5
5
const fields = [ 'location' , 'sizes.width' , 'sizes.height' , 'sizes.retina' , 'sizes.ratio' ]
6
6
const dir = {
@@ -9,21 +9,20 @@ const dir = {
9
9
defaultImages : './src/img/default/' ,
10
10
}
11
11
const path = {
12
- imagesSizesCsv : dir . conf + ' images-sizes.csv' ,
13
- imagesSizesJson : dir . conf + ' image-sizes.json' ,
14
- imageLocationsJson : dir . conf + ' image-locations.json' ,
12
+ imagesSizesCsv : ` ${ dir . conf } images-sizes.csv` ,
13
+ imagesSizesJson : ` ${ dir . conf } image-sizes.json` ,
14
+ imageLocationsJson : ` ${ dir . conf } image-locations.json` ,
15
15
}
16
16
const regex = {
17
17
srcset : / d a t a - s r c s e t = " ( .[ ^ " ] * ) " / gm,
18
18
crop : / c r o p = " ( .[ ^ " ] * ) " / gm,
19
19
img : / i m g - \d * - \d * / gm,
20
20
}
21
- const imageSettings = {
22
- fill : '#f1e25b' ,
23
- logo : './src/img/static/logo-beapi.svg' ,
24
- logoOpacity : 1 ,
25
- logoScale : 0.5 ,
26
- }
21
+ //the default image
22
+ const defaultFile = './src/img/static/default.jpg'
23
+ // the default image quality
24
+ const compression = 70
25
+
27
26
const locations = { }
28
27
const sizes = { }
29
28
@@ -36,7 +35,7 @@ let nbSizes = 0
36
35
* @return {array }
37
36
*/
38
37
function getTemplateFileNames ( ) {
39
- return fs . readdirSync ( dir . tpl ) . filter ( function ( tpl ) {
38
+ return fs . readdirSync ( dir . tpl ) . filter ( ( tpl ) => {
40
39
if ( tpl !== 'default-picture.tpl' && tpl !== 'default-picture-caption.tpl' ) {
41
40
return tpl
42
41
}
@@ -77,7 +76,7 @@ function removeFileExtension(name) {
77
76
* @return {String }
78
77
*/
79
78
function getDefaultImgName ( str ) {
80
- return str . replace ( 'img' , 'default' ) + ' .jpg'
79
+ return ` ${ str . replace ( 'img' , 'default' ) } .jpg`
81
80
}
82
81
83
82
/**
@@ -114,7 +113,7 @@ function createFile(filename, json) {
114
113
function imageLocationsFromTpl ( ) {
115
114
const templateFileNames = getTemplateFileNames ( )
116
115
117
- templateFileNames . forEach ( function ( tplName ) {
116
+ templateFileNames . forEach ( ( tplName ) => {
118
117
nbLocations += 1
119
118
const tplContent = getTemplateFileContent ( tplName )
120
119
const srcsetArr = tplContent . match ( regex . srcset )
@@ -146,13 +145,12 @@ function imageLocationsFromTpl() {
146
145
sizes [ size ] = {
147
146
width : splitSize [ 1 ] ,
148
147
height : splitSize [ 2 ] ,
149
- crop : crop ,
148
+ crop,
150
149
}
151
150
152
151
nbSizes += 1
153
152
}
154
153
155
- // console.log('[imageLocationsFromTpl]', CROP_STORE.length - 1, size, crop)
156
154
storage . srcsets . push ( srcsetObj )
157
155
storage . default_img = getDefaultImgName ( size )
158
156
storage . img_base = size
@@ -172,8 +170,9 @@ function imageLocationsFromTpl() {
172
170
* @returns {void }
173
171
*/
174
172
function generateDefaultImage ( sizes , filename ) {
173
+ const outputFile = dir . defaultImages + filename
175
174
try {
176
- if ( fs . existsSync ( dir . defaultImages + filename ) ) {
175
+ if ( fs . existsSync ( outputFile ) ) {
177
176
return
178
177
}
179
178
@@ -184,30 +183,19 @@ function generateDefaultImage(sizes, filename) {
184
183
return
185
184
}
186
185
187
- const canvas = createCanvas ( width , height )
188
- const context = canvas . getContext ( '2d' )
189
-
190
- context . fillStyle = imageSettings . fill
191
- context . fillRect ( 0 , 0 , width , height )
192
-
193
- loadImage ( imageSettings . logo ) . then ( ( image ) => {
194
- let logoHeight = height * imageSettings . logoScale
195
- let logoWidth = ( logoHeight * image . naturalWidth ) / image . naturalHeight
196
-
197
- if ( image . naturalWidth > image . naturalHeight ) {
198
- logoWidth = width * imageSettings . logoScale
199
- logoHeight = ( logoWidth * image . naturalHeight ) / image . naturalWidth
200
- }
201
-
202
- context . globalAlpha = imageSettings . logoOpacity
203
- context . drawImage ( image , ( width - logoWidth ) / 2 , ( height - logoHeight ) / 2 , logoWidth , logoHeight )
204
-
205
- const buffer = canvas . toBuffer ( 'image/jpeg' )
206
-
207
- if ( typeof buffer !== 'undefined' ) {
208
- fs . writeFileSync ( dir . defaultImages + filename , buffer )
209
- }
210
- } )
186
+ sharp ( defaultFile )
187
+ . resize ( width , height , {
188
+ fit : 'cover' ,
189
+ position : 'center' ,
190
+ } )
191
+ . jpeg ( { quality : compression , chromaSubsampling : '4:4:4' } )
192
+ . toFile ( outputFile , ( err , info ) => {
193
+ if ( err ) {
194
+ console . error ( err )
195
+ } else {
196
+ console . log ( `Created ${ info . width } x${ info . height } image at ${ outputFile } ` )
197
+ }
198
+ } )
211
199
} catch ( err ) {
212
200
console . error ( err )
213
201
}
@@ -239,8 +227,8 @@ function exportCSV() {
239
227
240
228
CSVObj . sizes . push ( {
241
229
retina : val . srcset === '2x' ? '✓' : '×' ,
242
- width : splitSize [ 1 ] + 'px' ,
243
- height : splitSize [ 2 ] + 'px' ,
230
+ width : ` ${ splitSize [ 1 ] } px` ,
231
+ height : ` ${ splitSize [ 2 ] } px` ,
244
232
ratio : splitSize [ 1 ] / splitSize [ 2 ] ,
245
233
} )
246
234
} )
0 commit comments