Nexchan commited on
Commit
f53f1f1
1 Parent(s): edc7eca

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +12 -16
index.js CHANGED
@@ -289,7 +289,6 @@ async function downloadImage(image, tempDir, instanceID) {
289
  const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
290
  await writeFileAsync(imagePath, response.data);
291
 
292
- // Mengurangi ukuran gambar sekitar 30%
293
  const imageHeight = await getImageHeight(imagePath);
294
  const newHeight = Math.floor(imageHeight * 0.7);
295
  const command = `convert ${imagePath} -resize 720x${newHeight}! -quality 75 -background white -gravity center -extent 720x${newHeight} ${imagePath}`;
@@ -304,6 +303,7 @@ async function getImageHeight(imagePath) {
304
  }
305
 
306
  async function processImages(imgList, tempDir, instanceID) {
 
307
  let partIndex = 0;
308
  let partImages = [];
309
 
@@ -311,28 +311,21 @@ async function processImages(imgList, tempDir, instanceID) {
311
  const imagePath = await downloadImage(imgList[i], tempDir, instanceID);
312
  partImages.push(imagePath);
313
 
314
- if (await getTotalHeight(partImages) > 64000) {
315
- await combineAndSave(partImages, partIndex, tempDir);
316
  partImages = [];
317
  partIndex++;
318
  }
319
  }
320
 
 
321
  if (partImages.length > 0) {
322
- await combineAndSave(partImages, partIndex, tempDir);
323
  }
324
  }
325
 
326
- async function getTotalHeight(imagePaths) {
327
- let totalHeight = 0;
328
- for (const imagePath of imagePaths) {
329
- totalHeight += await getImageHeight(imagePath);
330
- }
331
- return totalHeight;
332
- }
333
-
334
- async function combineAndSave(imagePaths, partIndex, tempDir) {
335
- const combinedImagePath = path.join(tempDir, `combined_part_${partIndex}.jpg`);
336
  const command = `convert ${imagePaths.join(' ')} -append -quality 75 ${combinedImagePath}`;
337
  await execPromise(command);
338
 
@@ -342,10 +335,13 @@ async function combineAndSave(imagePaths, partIndex, tempDir) {
342
  }
343
 
344
  async function createPDF(instanceID, tempDir) {
345
- const combinedParts = fs.readdirSync(tempDir).filter(file => file.startsWith('combined_part_'));
 
 
346
  const pdfPath = path.join(tempDir, `${instanceID}.pdf`);
347
- const createPDFCommand = `convert ${combinedParts.map(file => path.join(tempDir, file)).join(' ')} ${pdfPath}`;
348
  await execPromise(createPDFCommand);
 
349
  return pdfPath;
350
  }
351
 
 
289
  const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
290
  await writeFileAsync(imagePath, response.data);
291
 
 
292
  const imageHeight = await getImageHeight(imagePath);
293
  const newHeight = Math.floor(imageHeight * 0.7);
294
  const command = `convert ${imagePath} -resize 720x${newHeight}! -quality 75 -background white -gravity center -extent 720x${newHeight} ${imagePath}`;
 
303
  }
304
 
305
  async function processImages(imgList, tempDir, instanceID) {
306
+ const maxImagesPerPage = 10; // Maksimal 10 gambar per halaman
307
  let partIndex = 0;
308
  let partImages = [];
309
 
 
311
  const imagePath = await downloadImage(imgList[i], tempDir, instanceID);
312
  partImages.push(imagePath);
313
 
314
+ if (partImages.length >= maxImagesPerPage) {
315
+ await combineAndSave(partImages, partIndex, tempDir, instanceID);
316
  partImages = [];
317
  partIndex++;
318
  }
319
  }
320
 
321
+ // Jika masih ada gambar yang belum diproses
322
  if (partImages.length > 0) {
323
+ await combineAndSave(partImages, partIndex, tempDir, instanceID);
324
  }
325
  }
326
 
327
+ async function combineAndSave(imagePaths, partIndex, tempDir, instanceID) {
328
+ const combinedImagePath = path.join(tempDir, `combined_part_${instanceID}_${partIndex}.jpg`);
 
 
 
 
 
 
 
 
329
  const command = `convert ${imagePaths.join(' ')} -append -quality 75 ${combinedImagePath}`;
330
  await execPromise(command);
331
 
 
335
  }
336
 
337
  async function createPDF(instanceID, tempDir) {
338
+ const combinedParts = fs.readdirSync(tempDir).filter(file => file.startsWith(`combined_part_${instanceID}_`));
339
+ const combinedImagesPath = combinedParts.map(file => path.join(tempDir, file)).join(' ');
340
+
341
  const pdfPath = path.join(tempDir, `${instanceID}.pdf`);
342
+ const createPDFCommand = `convert ${combinedImagesPath} ${pdfPath}`;
343
  await execPromise(createPDFCommand);
344
+
345
  return pdfPath;
346
  }
347