Skip to content

Resize blog posts images #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
523 changes: 498 additions & 25 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
},
"scripts": {
"docusaurus": "docusaurus",
"prestart": "node scripts/resize-images.js",
"prebuild": "node scripts/resize-images.js",
"start": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
Expand All @@ -33,7 +35,7 @@
"clsx": "^2.0.0",
"curl": "^0.1.4",
"fs": "^0.0.1-security",
"image-size": "^1.1.1",
"image-size": "^1.2.1",
"jimp": "^0.22.12",
"js-yaml": "^4.1.0",
"nodejs": "^0.0.0",
Expand All @@ -48,6 +50,7 @@
"react-slick": "^0.30.2",
"reactjs-popup": "^2.0.6",
"request": "^2.88.2",
"sharp": "^0.34.2",
"slick-carousel": "^1.8.1"
},
"devDependencies": {
Expand All @@ -72,4 +75,4 @@
"engines": {
"node": "18.x"
}
}
}
49 changes: 49 additions & 0 deletions scripts/resize-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const sizeOf = require('image-size');
const inputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'full-size-images');
const outputDir = path.join(__dirname, '../static', 'img', 'blogposts', 'resized-images');
const containerHeight = 180;
const containerWidth = 273;
const containerAspectRatio = containerWidth / containerHeight;
console.log('container Aspect Ratio:', containerAspectRatio);

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

fs.readdirSync(inputDir).forEach((file) => {
const inputPath = path.join(inputDir, file);
if (fs.existsSync(inputPath)) {
const dimensions = sizeOf(inputPath);

const aspectRatio = dimensions.width / dimensions.height;
const outputPath = path.join(outputDir, file);
let targetWidth, targetHeight
let width;
if (aspectRatio <= containerAspectRatio) {
targetHeight = containerHeight
targetWidth = Math.round(targetHeight * aspectRatio);
} else {
targetWidth = containerWidth*2; /* factor 2 to increase the resolution*/
targetHeight = Math.round(targetWidth / aspectRatio);
}

// Check that the image fits within the container
if (targetWidth > containerWidth * 2) {
targetWidth = containerWidth * 2;
targetHeight = Math.round(targetWidth / aspectRatio);
}
if (targetHeight > containerHeight) {
targetHeight = containerHeight;
targetWidth = Math.round(targetHeight * aspectRatio);
}

if (/\.(png|jpg)$/i.test(file)) {
sharp(inputPath)
.resize(targetWidth, targetHeight)
.toFile(outputPath)
}
}
});
2 changes: 0 additions & 2 deletions src/components/blog/BlogpostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export default function BlogpostCard({ blogpost, timeIndex }) {
<img
src={useBaseUrl(blogpost.image)}
id={blogpost.imageID}
width={blogpost.imageRenderedWidth}
height={blogpost.imageRenderedHeight}
alt={"Illustration for the blog post."}
/>
</div>
Expand Down
839 changes: 0 additions & 839 deletions src/components/blog/_config.yml

This file was deleted.

Loading