Skip to content

Commit

Permalink
feat: use sharp to transform image size
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgevrgs committed May 15, 2024
1 parent 1b36241 commit 3cafc62
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
1 change: 0 additions & 1 deletion apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/react": "18.0.19",
"@types/react-dom": "18.0.6",
"@types/react-test-renderer": "18.0.0",
"@types/sharp": "0.31.0",
"@vitejs/plugin-react": "2.1.0",
"autoprefixer": "10.4.9",
"eslint": "8.23.1",
Expand Down
46 changes: 21 additions & 25 deletions libs/backend/infrastructure/controllers/images.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import { NextApiRequest, NextApiResponse } from 'next';
// import sharp from 'sharp';
import sharp from 'sharp';
import { findOneByNameBucket } from '../../application';

export async function getImageByNameController(
Expand All @@ -19,16 +19,14 @@ export async function getImageByNameController(
fileName.replace(/.webp$/, '.png')
);

// const output = sharp().webp({ quality: 80 }).resize({
// fit: 'contain',
// width: 300,
// height: 300,
// });
const output = sharp().webp({ quality: 80 }).resize({
fit: 'contain',
width: 300,
height: 300,
});

// res.setHeader('Content-Type', 'image/webp');
file
// .pipe(output)
.pipe(res);
res.setHeader('Content-Type', 'image/webp');
file.pipe(output).pipe(res);
}

export async function getCoverByNameController(
Expand All @@ -44,19 +42,17 @@ export async function getCoverByNameController(
fileName.replace(/.webp$/, '.png')
);

// const output = sharp()
// .webp({ quality: 80 })
// .resize({
// fit: 'cover',
// width: 1280,
// height: 720,
// })
// .extend({
// background: { r: 255, g: 255, b: 255, alpha: 0.8 },
// });

// res.setHeader('Content-Type', 'image/webp');
file
// .pipe(output)
.pipe(res);
const output = sharp()
.webp({ quality: 80 })
.resize({
fit: 'cover',
width: 1280,
height: 720,
})
.extend({
background: { r: 255, g: 255, b: 255, alpha: 0.8 },
});

res.setHeader('Content-Type', 'image/webp');
file.pipe(output).pipe(res);
}
4 changes: 1 addition & 3 deletions libs/frontend/infrastructure/components/featured-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function FeaturedCard() {
<div className="contents lg:relative lg:w-full lg:max-w-5xl lg:mx-auto relative">
<div className="absolute inset-0 ">
<Image
// layout="fill"
layout="fill"
className="w-full h-full"
src={featuredCelebrity.picture
.replace('/images/', '/cover/')
Expand All @@ -23,8 +23,6 @@ export default function FeaturedCard() {
priority={true}
objectFit="cover"
objectPosition="right"
width={1280}
height={720}
/>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export default function Celebrity({ celebrity, listOption }: CelebrityProps) {
'sm:object-cover sm:w-full sm:h-full': isList,
})}
objectFit={isList ? 'contain' : 'fill'}
// layout="fill"
layout="fill"
objectPosition={isList ? 'left' : 'center'}
width={300}
height={300}
/>
</div>

Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3cafc62

Please sign in to comment.