Skip to content

Commit e0cec89

Browse files
Update resize-image.js script.
1 parent 3662d8d commit e0cec89

File tree

136 files changed

+31
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+31
-36
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ node_modules/
55
build/
66
.docusaurus
77
static/img/light-and-dark-pictures/
8-
statit/img/blogposts/resized-images
8+
static/img/blogposts/resized-images

scripts/resize-images.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,43 @@ const sharp = require('sharp');
22
const fs = require('fs');
33
const path = require('path');
44
const 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');
77
const containerHeight = 180;
88
const 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+
})
-5.75 MB
Binary file not shown.
-160 KB
Binary file not shown.
-24.7 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)