-
Notifications
You must be signed in to change notification settings - Fork 3
/
clip.js
120 lines (92 loc) · 3 KB
/
clip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const joinImages = require("join-images");
const fs = require('fs-extra');
// given the rows, and amount of images, determine how to split them
// for example if it's 9 images and 5 files, we want [2,2,2,2,1]
let distributeInteger = function* (total, divider) {
if (divider === 0) {
yield 0
} else {
let rest = total % divider
let result = total / divider
for (let i = 0; i < divider; i++) {
if (rest-- >0) {
yield Math.ceil(result)
} else {
yield Math.floor(result)
}
}
}
}
function range(start, end) {
var myArray = [];
for (var i = start; i <= end; i += 1) {
myArray.push(i);
}
return myArray;
};
async function clipSpriteThumbnail({
rows,
targetFileSize,
debug = false,
averageRowSizeInKb,
outputFileDirectory,
filename
}){
if(!debug){
const c = {
l : undefined
}
}
// used for creating the webvtt
const imagesWithRows = [];
const maximumRowsPerImage = Math.floor(targetFileSize/averageRowSizeInKb);
let howManyImages = Math.floor(rows / maximumRowsPerImage);
if(howManyImages === 0) howManyImages = 1;
let groups = []
for (let member of distributeInteger(rows, howManyImages)) {
groups.push(member)
}
c.l('groups')
c.l(groups);
// start current starting row at 1
let currentStartingRow = 1;
for (const [index, amountOfRows] of groups.entries()) {
const imageNumber = index + 1;
// increment the next starting row
const finishingRow = currentStartingRow + (amountOfRows - 1) // if you do only 1 row you finish same row
c.l('starting row, ending row');
c.l(currentStartingRow, finishingRow)
const rangeArray = range(currentStartingRow, finishingRow);
c.l('range array', rangeArray)
const horizontalImagesDirectory = `${outputFileDirectory}/processing/horizontalImages`;
const finalOutputPath = `${outputFileDirectory}/${filename}-${imageNumber}.webp`
if(rangeArray.length === 1){
const currentDirectory = `${horizontalImagesDirectory}/${rangeArray[0]}.webp`;
await fs.copy(currentDirectory, finalOutputPath)
} else {
let arrayOfImages = [];
for(const horizontalImageNumber of rangeArray){
arrayOfImages.push(`${horizontalImagesDirectory}/${horizontalImageNumber}.webp`);
}
c.l(arrayOfImages);
// create Sharp instance
const verticalJoinSharpInstance = await joinImages.joinImages(arrayOfImages, { direction: 'vertical'})
// save Sharp instance to file
const verticalJoinedImageResponse = await verticalJoinSharpInstance.toFile(finalOutputPath);
}
//
const webvttObject = {
startingRow: currentStartingRow,
finishingRow, // if you do only 1 row you finish same row
imageNumber,
amountOfRows
}
imagesWithRows.push(webvttObject);
// increment the starting row for next loop
currentStartingRow = currentStartingRow + amountOfRows;
}
c.l('images with rows');
c.l(imagesWithRows);
return imagesWithRows
}
module.exports = clipSpriteThumbnail