-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anshul
committed
Nov 22, 2020
1 parent
f361906
commit bbea9d8
Showing
4 changed files
with
82 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,66 @@ | ||
const { | ||
MozJPEG, | ||
Steps, | ||
FromURL, | ||
FromFile, | ||
FromStream, | ||
FromBuffer, | ||
FromURL, | ||
Constrain, | ||
ConstrainMode, | ||
ReSample, | ||
ConstrainHints, | ||
TransparentColor, | ||
BlackColor, | ||
SRGBColor, | ||
ColorType, | ||
} = require('..') | ||
// const fs = require('fs') | ||
|
||
// let step = new Steps(new FromURL('https://jpeg.org/images/jpeg2000-home.jpg')) | ||
// .constraintWithin(500, 500) | ||
// .branch((step) => | ||
// step | ||
// .constraintWithin(400, 400) | ||
// .branch((step) => | ||
// step | ||
// .constraintWithin(200, 200) | ||
// .rotate90() | ||
// .colorFilterGrayscaleFlat() | ||
// .encode(new FromFile('./branch_2.jpg'), new MozJPEG(80)) | ||
// ) | ||
// .copyRectangle( | ||
// (canvas) => | ||
// canvas.decode( | ||
// new FromStream(fs.createReadStream('./test.jpg')) | ||
// ), | ||
// { x: 0, y: 0, w: 100, h: 100 }, | ||
// 10, | ||
// 10 | ||
// ) | ||
// .encode(new FromFile('./branch.jpg'), new MozJPEG(80)) | ||
// ) | ||
// .constraintWithin(100, 100) | ||
// .rotate180() | ||
// step.encode(new FromBuffer(null, 'key'), new MozJPEG(80)) | ||
// .execute() | ||
// .then(console.log) | ||
// .catch(console.log) | ||
class Resize extends Steps { | ||
resize(source, output, sizes) { | ||
const [width, height] = sizes[0].split('x') | ||
const out = output.replace('{w}', width).replace('{h}', height) | ||
const nextSizes = sizes.slice(1) | ||
|
||
const test = new Steps(new FromFile('./test/test.jpg')) | ||
.constraintWithin(800, 800) | ||
.branch((step) => step.encode(new FromFile('large.jpeg'), new MozJPEG())) | ||
.branch((step) => | ||
step | ||
.constraintWithin(400, 400) | ||
.branch((step) => | ||
step | ||
.constraintWithin(200, 200) | ||
.branch((step) => | ||
step | ||
.constraintWithin(100, 100) | ||
.encode( | ||
new FromFile('performing.jpeg'), | ||
new MozJPEG() | ||
) | ||
) | ||
.encode(new FromFile('small.jpeg'), new MozJPEG()) | ||
this.branch((step) => { | ||
step.constrain( | ||
new Constrain(ConstrainMode.Within, width * 1, height * 1) | ||
) | ||
.encode(new FromFile('medium.jpeg'), new MozJPEG()) | ||
) | ||
.execute() | ||
|
||
// class Graph { | ||
// map | ||
|
||
// constructor() { | ||
// this.map = new Map() | ||
// } | ||
|
||
// addVertex(ver) { | ||
// this.map.set(ver, []) | ||
// } | ||
|
||
// addEdge(to, from) { | ||
// console.log(this.map, to, from) | ||
// this.map.get(from).push(to) | ||
// } | ||
// } | ||
|
||
// class Base { | ||
// graph | ||
// vertex | ||
// last | ||
|
||
// constructor() { | ||
// this.graph = new Graph() | ||
// this.vertex = [] | ||
// this.last = 0 | ||
// this.graph.addVertex(0) | ||
// this.vertex.push(0) | ||
// } | ||
|
||
// addEdge() { | ||
// this.graph.addVertex(this.vertex.length) | ||
// this.vertex.push(this.vertex.length) | ||
// this.graph.addEdge(this.vertex.length - 1, this.last) | ||
// this.last = this.vertex.length - 1 | ||
// return this | ||
// } | ||
if (nextSizes.length) { | ||
step.resize(source, output, nextSizes) | ||
} | ||
step.encode( | ||
new FromFile(out), | ||
new MozJPEG(90, { | ||
isProgressive: true, | ||
matte: new SRGBColor(ColorType.Hex, '00ffff'), | ||
}) | ||
) | ||
}) | ||
|
||
// branch(f) { | ||
// let last = this.last | ||
// f(this) | ||
// this.last = last | ||
// return this | ||
// } | ||
// } | ||
return this | ||
} | ||
} | ||
|
||
// let base = new Base() | ||
const start = async function () { | ||
const [src, out, min, max] = process.argv.slice(2) | ||
const sizes = [ | ||
'1920x1920', | ||
'1080x1080', | ||
'720x720', | ||
'480x480', | ||
'360x360', | ||
'120x120', | ||
'60x60', | ||
] | ||
|
||
// base.addEdge() | ||
// .branch((step) => | ||
// step | ||
// .addEdge() | ||
// .addEdge() | ||
// .branch((step) => step.addEdge().addEdge()) | ||
// .addEdge() | ||
// ) | ||
// .addEdge() | ||
// .addEdge() | ||
const input = src.indexOf('://') > -1 ? new FromURL(src) : new FromFile(src) | ||
for await (const size of sizes) { | ||
if (min || max) { | ||
const [width, height] = size.split('x') | ||
if ((min && width * 1 < min * 1) || (max && width * 1 > max * 1)) { | ||
continue | ||
} | ||
} | ||
console.log('size ' + size) | ||
await new Resize(input).resize(src, out, [size]).execute() | ||
} | ||
} | ||
|
||
// console.log(base.graph) | ||
start() |