Skip to content

Commit 1eef0a4

Browse files
committed
fix: upgrade Jimp to newest version as a potential solutions to regenerate issues (#2476)
1 parent e552ade commit 1eef0a4

4 files changed

Lines changed: 458 additions & 490 deletions

File tree

app/back-end/builddata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"version": "0.47.4",
3-
"build": 17408
3+
"build": 17411
44
}

app/back-end/image.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const normalizePath = require('normalize-path');
1212
const Themes = require('./themes.js');
1313
const Utils = require('./helpers/utils.js');
1414
const slug = require('./helpers/slug');
15-
const Jimp = require('jimp');
15+
const { Jimp } = require('jimp');
1616
// Default config
1717
const defaultAstCurrentSiteConfig = require('./../config/AST.currentSite.config');
1818
let sharp = require('sharp');
@@ -365,47 +365,41 @@ class Image extends Model {
365365

366366
if (finalHeight === 'auto') {
367367
finalHeight = null;
368-
369-
if (this.shouldUseJimp()) {
370-
finalHeight = Jimp.AUTO;
371-
}
372368
}
373369

374370
if (finalWidth === 'auto') {
375371
finalWidth = null;
376-
377-
if (this.shouldUseJimp()) {
378-
finalWidth = Jimp.AUTO;
379-
}
380372
}
381373

382374
if (cropImage) {
383375
if (this.shouldUseJimp()) {
384-
result = new Promise ((resolve, reject) => {
385-
Jimp.read(originalPath, function (err, image) {
386-
if (err) {
387-
reject(err);
388-
}
389-
376+
result = new Promise (async (resolve, reject) => {
377+
try {
378+
let image = await Jimp.read(originalPath);
390379
console.log('JIMP COVER', finalWidth, ' x ', finalHeight);
391380

392-
if (finalWidth === Jimp.AUTO || finalHeight === Jimp.AUTO) {
393-
image.resize(finalWidth, finalHeight)
394-
.quality(imagesQuality)
395-
.write(destinationPath, function() {
396-
resolve(destinationPath);
397-
});
381+
if (finalWidth === null || finalHeight === null) {
382+
let resizeOptions = {};
383+
if (finalWidth !== null) {
384+
resizeOptions.w = finalWidth;
385+
}
386+
387+
if (finalHeight !== null) {
388+
resizeOptions.h = finalHeight;
389+
}
390+
391+
image.resize(resizeOptions);
392+
await image.write(destinationPath, { quality: imagesQuality });
393+
resolve(destinationPath);
398394
} else {
399-
image.cover(finalWidth, finalHeight)
400-
.quality(imagesQuality)
401-
.write(destinationPath, function() {
402-
resolve(destinationPath);
403-
});
395+
image.cover({ w: finalWidth, h: finalHeight });
396+
await image.write(destinationPath, { quality: imagesQuality });
397+
resolve(destinationPath);
404398
}
405-
}).catch(err => {
399+
} catch (err) {
406400
console.log(err);
407401
reject(err);
408-
});
402+
}
409403
});
410404
} else {
411405
result = new Promise ((resolve, reject) => {
@@ -466,22 +460,26 @@ class Image extends Model {
466460
}
467461
} else {
468462
if (this.shouldUseJimp()) {
469-
result = new Promise ((resolve, reject) => {
470-
Jimp.read(originalPath, function (err, image) {
471-
if (err) {
472-
reject(err);
463+
result = new Promise (async (resolve, reject) => {
464+
try {
465+
const image = await Jimp.read(originalPath);
466+
console.log('JIMP RESIZE/SCALE TO FIT', finalWidth, ' x ', finalHeight);
467+
468+
if (finalWidth && finalHeight) {
469+
image.scaleToFit({ w: finalWidth, h: finalHeight });
470+
} else if (finalWidth) {
471+
image.resize({ w: finalWidth });
472+
} else if (finalHeight) {
473+
image.resize({ h: finalHeight });
473474
}
474475

475-
console.log('JIMP SCALE TO FIT', finalWidth, ' x ', finalHeight);
476-
image.scaleToFit(finalWidth, finalHeight)
477-
.quality(imagesQuality)
478-
.write(destinationPath, function() {
479-
resolve(destinationPath)
480-
});
481-
});
482-
}).catch(err => {
483-
console.log(err);
484-
reject(err);
476+
await image.write(destinationPath, { quality: imagesQuality });
477+
478+
resolve(destinationPath);
479+
} catch (err) {
480+
console.error(err);
481+
reject(err);
482+
}
485483
});
486484
} else {
487485
result = new Promise ((resolve, reject) => {

0 commit comments

Comments
 (0)