Skip to content

Commit 0d94a8d

Browse files
Add a script to resize the pictures for the blogposts page.
Add the script in the package.json as prestart and prebuild. Update blogpostsDetails.json and BlogpostCard.tsx. Remove unused or obsolote files in src/components/blog. Remove gif files and replace them by png images. Convert svg images to png ones.
1 parent 66e6e04 commit 0d94a8d

File tree

298 files changed

+820
-12292
lines changed

Some content is hidden

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

298 files changed

+820
-12292
lines changed

package-lock.json

Lines changed: 498 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
},
1010
"scripts": {
1111
"docusaurus": "docusaurus",
12+
"prestart": "node scripts/resize-images.js",
13+
"prebuild": "node scripts/resize-images.js",
1214
"start": "docusaurus start",
1315
"build": "docusaurus build",
1416
"swizzle": "docusaurus swizzle",
@@ -33,7 +35,7 @@
3335
"clsx": "^2.0.0",
3436
"curl": "^0.1.4",
3537
"fs": "^0.0.1-security",
36-
"image-size": "^1.1.1",
38+
"image-size": "^1.2.1",
3739
"jimp": "^0.22.12",
3840
"js-yaml": "^4.1.0",
3941
"nodejs": "^0.0.0",
@@ -48,6 +50,7 @@
4850
"react-slick": "^0.30.2",
4951
"reactjs-popup": "^2.0.6",
5052
"request": "^2.88.2",
53+
"sharp": "^0.34.2",
5154
"slick-carousel": "^1.8.1"
5255
},
5356
"devDependencies": {
@@ -72,4 +75,4 @@
7275
"engines": {
7376
"node": "18.x"
7477
}
75-
}
78+
}

scripts/resize-images.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const sharp = require('sharp');
2+
const fs = require('fs');
3+
const path = require('path');
4+
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');
7+
const containerHeight = 180;
8+
const containerWidth = 273;
9+
const containerAspectRatio = containerWidth / containerHeight;
10+
console.log('container Aspect Ratio:', containerAspectRatio);
11+
12+
if (!fs.existsSync(outputDir)) {
13+
fs.mkdirSync(outputDir, { recursive: true });
14+
}
15+
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+
}
38+
if (targetHeight > containerHeight) {
39+
targetHeight = containerHeight;
40+
targetWidth = Math.round(targetHeight * aspectRatio);
41+
}
42+
43+
if (/\.(png|jpg)$/i.test(file)) {
44+
sharp(inputPath)
45+
.resize(targetWidth, targetHeight)
46+
.toFile(outputPath)
47+
}
48+
}
49+
});

src/components/blog/BlogpostCard.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export default function BlogpostCard({ blogpost, timeIndex }) {
3232
<img
3333
src={useBaseUrl(blogpost.image)}
3434
id={blogpost.imageID}
35-
width={blogpost.imageRenderedWidth}
36-
height={blogpost.imageRenderedHeight}
3735
alt={"Illustration for the blog post."}
3836
/>
3937
</div>

src/components/blog/_config.yml

Lines changed: 0 additions & 839 deletions
This file was deleted.

0 commit comments

Comments
 (0)