Skip to content

Commit 89e4857

Browse files
committed
feat (sharp) add sharp as image generator for default images
1 parent 71b8a8e commit 89e4857

File tree

5 files changed

+239
-198
lines changed

5 files changed

+239
-198
lines changed

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
nodeLinker: pnp
1+
nodeLinker: node-modules
22

33
yarnPath: .yarn/releases/yarn-3.2.3.cjs

config/image-sizes.js

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs')
22
const ora = require('ora')
3-
const { createCanvas, loadImage } = require('canvas')
3+
const sharp = require('sharp')
44
const Json2csvParser = require('json2csv').Parser
55
const fields = ['location', 'sizes.width', 'sizes.height', 'sizes.retina', 'sizes.ratio']
66
const dir = {
@@ -9,21 +9,20 @@ const dir = {
99
defaultImages: './src/img/default/',
1010
}
1111
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`,
1515
}
1616
const regex = {
1717
srcset: /data-srcset="(.[^"]*)"/gm,
1818
crop: /crop="(.[^"]*)"/gm,
1919
img: /img-\d*-\d*/gm,
2020
}
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+
2726
const locations = {}
2827
const sizes = {}
2928

@@ -36,7 +35,7 @@ let nbSizes = 0
3635
* @return {array}
3736
*/
3837
function getTemplateFileNames() {
39-
return fs.readdirSync(dir.tpl).filter(function (tpl) {
38+
return fs.readdirSync(dir.tpl).filter((tpl) => {
4039
if (tpl !== 'default-picture.tpl' && tpl !== 'default-picture-caption.tpl') {
4140
return tpl
4241
}
@@ -77,7 +76,7 @@ function removeFileExtension(name) {
7776
* @return {String}
7877
*/
7978
function getDefaultImgName(str) {
80-
return str.replace('img', 'default') + '.jpg'
79+
return `${str.replace('img', 'default')}.jpg`
8180
}
8281

8382
/**
@@ -114,7 +113,7 @@ function createFile(filename, json) {
114113
function imageLocationsFromTpl() {
115114
const templateFileNames = getTemplateFileNames()
116115

117-
templateFileNames.forEach(function (tplName) {
116+
templateFileNames.forEach((tplName) => {
118117
nbLocations += 1
119118
const tplContent = getTemplateFileContent(tplName)
120119
const srcsetArr = tplContent.match(regex.srcset)
@@ -146,13 +145,12 @@ function imageLocationsFromTpl() {
146145
sizes[size] = {
147146
width: splitSize[1],
148147
height: splitSize[2],
149-
crop: crop,
148+
crop,
150149
}
151150

152151
nbSizes += 1
153152
}
154153

155-
// console.log('[imageLocationsFromTpl]', CROP_STORE.length - 1, size, crop)
156154
storage.srcsets.push(srcsetObj)
157155
storage.default_img = getDefaultImgName(size)
158156
storage.img_base = size
@@ -172,8 +170,9 @@ function imageLocationsFromTpl() {
172170
* @returns {void}
173171
*/
174172
function generateDefaultImage(sizes, filename) {
173+
const outputFile = dir.defaultImages + filename
175174
try {
176-
if (fs.existsSync(dir.defaultImages + filename)) {
175+
if (fs.existsSync(outputFile)) {
177176
return
178177
}
179178

@@ -184,30 +183,19 @@ function generateDefaultImage(sizes, filename) {
184183
return
185184
}
186185

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+
})
211199
} catch (err) {
212200
console.error(err)
213201
}
@@ -239,8 +227,8 @@ function exportCSV() {
239227

240228
CSVObj.sizes.push({
241229
retina: val.srcset === '2x' ? '✓' : '×',
242-
width: splitSize[1] + 'px',
243-
height: splitSize[2] + 'px',
230+
width: `${splitSize[1]}px`,
231+
height: `${splitSize[2]}px`,
244232
ratio: splitSize[1] / splitSize[2],
245233
})
246234
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"dependencies": {
1717
"lazysizes": "^5.3.2",
1818
"oneloop.js": "^5.0.0",
19+
"sharp": "^0.32.0",
1920
"what-input": "^5.2.10"
2021
},
2122
"devDependencies": {
@@ -26,7 +27,6 @@
2627
"@wordpress/stylelint-config": "^21.0.0",
2728
"browser-sync": "^2.27.10",
2829
"browser-sync-webpack-plugin": "^2.3.0",
29-
"canvas": "^2.9.2",
3030
"clean-webpack-plugin": "^4.0.0-alpha.0",
3131
"css-loader": "^5.2.4",
3232
"cssnano": "^5.0.1",
File renamed without changes.

0 commit comments

Comments
 (0)