@@ -2,48 +2,43 @@ const sharp = require('sharp');
22const fs = require('fs');
33const path = require('path');
44const sizeOf = require('image-size');
5- const inputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'full-size-images');
6- const outputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'resized-images');
5+ const fullSizeDir = path.join(__dirname, '../static', 'img', 'blogposts', 'full-size-images');
6+ const reducedSizeDir = path.join(__dirname, '../static', 'img', 'blogposts', 'resized-images');
77const containerHeight = 180;
88const containerWidth = 273;
9- const containerAspectRatio = containerWidth / containerHeight;
10- console.log('container Aspect Ratio:', containerAspectRatio);
119
12- if (!fs.existsSync(outputDir )) {
13- fs.mkdirSync(outputDir , { recursive: true });
10+ if (!fs.existsSync(reducedSizeDir )) {
11+ fs.mkdirSync(reducedSizeDir , { recursive: true });
1412}
1513
16- fs.readdirSync(inputDir).forEach((file) => {
17- const inputPath = path.join(inputDir, file);
18- if (fs.existsSync(inputPath)) {
19- const dimensions = sizeOf(inputPath);
20-
21- const aspectRatio = dimensions.width / dimensions.height;
22- const outputPath = path.join(outputDir, file);
23- let targetWidth, targetHeight
24- let width;
25- if (aspectRatio <= containerAspectRatio) {
26- targetHeight = containerHeight
27- targetWidth = Math.round(targetHeight * aspectRatio);
28- } else {
29- targetWidth = containerWidth*2; /* factor 2 to increase the resolution*/
30- targetHeight = Math.round(targetWidth / aspectRatio);
31- }
32-
33- // Check that the image fits within the container
34- if (targetWidth > containerWidth * 2) {
35- targetWidth = containerWidth * 2;
36- targetHeight = Math.round(targetWidth / aspectRatio);
37- }
14+ function calculateImageSize(fullSizePath) {
15+ if (fs.existsSync(fullSizePath)) {
16+ const width = sizeOf(fullSizePath).width;
17+ const height = sizeOf(fullSizePath).height;
18+ let targetWidth, targetHeight;
19+ if (width * containerHeight > containerWidth * height) {
20+ targetWidth = containerWidth * 2;
21+ targetHeight = Math.round(targetWidth / width * height);
3822 if (targetHeight > containerHeight) {
3923 targetHeight = containerHeight;
40- targetWidth = Math.round(targetHeight * aspectRatio );
24+ targetWidth = Math.round(targetHeight * width / height );
4125 }
42-
43- if (/\.(png|jpg)$/i.test(file)) {
44- sharp(inputPath)
45- .resize(targetWidth, targetHeight)
46- .toFile(outputPath)
4726 }
27+ else {
28+ targetHeight = containerHeight;
29+ targetWidth = Math.round(targetHeight / width * height);
30+ }
31+ return [targetWidth, targetHeight];
32+ }
33+ }
34+
35+ fs.readdirSync(fullSizeDir).forEach((file) => {
36+ const fullSizePath = path.join(fullSizeDir, file);
37+ const reducedSizePath = path.join(reducedSizeDir, file);
38+ const [targetWidth, targetHeight] = calculateImageSize(fullSizePath);
39+ if (/\.(png|jpg)$/i.test(file)) {
40+ sharp(fullSizePath)
41+ .resize(targetWidth, targetHeight)
42+ .toFile(reducedSizePath)
4843 }
49- });
44+ })
0 commit comments