Skip to content

Commit

Permalink
Added jpg compression / quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Luen committed Nov 5, 2021
1 parent 8dc9a79 commit 7ba492d
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ app.get('*', function(req, res) {
//const LOGO = "https://wanderstories.space/content/images/size/w1000/2021/11/Wanderstories-publication-logo.png"
const LOGO = __dirname + "/Wanderstories-logo.png"

const JIMP_QUALITY = 60
const LOGO_MARGIN_PERCENTAGE = 5
var LOCATION = 'Center' // or 'Bottom Right'

const ACCEPTED = [
'jpg',
Expand Down Expand Up @@ -49,49 +49,55 @@ app.get('*', function(req, res) {
Jimp.read(LOGO)
])

if (LOCATION == 'Center') {
logo.resize(image.bitmap.width / 5, Jimp.AUTO)

const X = (image.bitmap.width / 2) - (logo.bitmap.width / 2)
const Y = (image.bitmap.height / 2) - (logo.bitmap.height / 2)

/*Jimp.BLEND_SOURCE_OVER;
Jimp.BLEND_DESTINATION_OVER;
Jimp.BLEND_MULTIPLY;
Jimp.BLEND_ADD; ---
Jimp.BLEND_SCREEN;
Jimp.BLEND_OVERLAY;
Jimp.BLEND_DARKEN;
Jimp.BLEND_LIGHTEN;
Jimp.BLEND_HARDLIGHT;
Jimp.BLEND_DIFFERENCE;
Jimp.BLEND_EXCLUSION;
*/

return image.composite(logo, X, Y, {
mode: Jimp.BLEND_ADD,
opacitySource: 0.2,
opacityDest: 1
})
} else { // Bottom Right
logo.resize(image.bitmap.width / 10, Jimp.AUTO)

const xMargin = (image.bitmap.width * LOGO_MARGIN_PERCENTAGE) / 100
const yMargin = (image.bitmap.height * LOGO_MARGIN_PERCENTAGE) / 100

const X = image.bitmap.width - logo.bitmap.width - xMargin
const Y = image.bitmap.height - logo.bitmap.height - yMargin

return image.composite(logo, X, Y, {
mode: Jimp.BLEND_SCREEN,
opacitySource: 0.5,
opacityDest: 1
})
}
///////////////////
///// Center //////
///////////////////
logo.resize(image.bitmap.width / 5, Jimp.AUTO)

const X = (image.bitmap.width / 2) - (logo.bitmap.width / 2)
const Y = (image.bitmap.height / 2) - (logo.bitmap.height / 2)

/* // BLEND MODES
Jimp.BLEND_SOURCE_OVER;
Jimp.BLEND_DESTINATION_OVER;
Jimp.BLEND_MULTIPLY;
Jimp.BLEND_ADD; ---
Jimp.BLEND_SCREEN;
Jimp.BLEND_OVERLAY;
Jimp.BLEND_DARKEN;
Jimp.BLEND_LIGHTEN;
Jimp.BLEND_HARDLIGHT;
Jimp.BLEND_DIFFERENCE;
Jimp.BLEND_EXCLUSION;
*/

return image.composite(logo, X, Y, {
mode: Jimp.BLEND_ADD,
opacitySource: 0.2,
opacityDest: 1
})
///////////////////
// Bottom Right ///
///////////////////
/*
logo.resize(image.bitmap.width / 10, Jimp.AUTO)
const xMargin = (image.bitmap.width * LOGO_MARGIN_PERCENTAGE) / 100
const yMargin = (image.bitmap.height * LOGO_MARGIN_PERCENTAGE) / 100
const X = image.bitmap.width - logo.bitmap.width - xMargin
const Y = image.bitmap.height - logo.bitmap.height - yMargin
return image.composite(logo, X, Y, {
mode: Jimp.BLEND_SCREEN,
opacitySource: 0.5,
opacityDest: 1
})
*/
}

// write file to server and send file
main().then(image => image.writeAsync(PATH)).then(() => {
main().then(image => image.quality(JIMP_QUALITY).writeAsync(PATH)).then(() => {
res.sendFile(PATH)
})
}
Expand Down

0 comments on commit 7ba492d

Please sign in to comment.