Skip to content

Commit

Permalink
png to webp, and properly size images
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiboSoftwareDev committed Jan 11, 2025
1 parent 4aaa975 commit e24002b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 2 deletions.
Binary file removed src/assets/editor_example_1.png
Binary file not shown.
Binary file added src/assets/editor_example_1.webp
Binary file not shown.
Binary file removed src/assets/editor_example_1_more_square.png
Binary file not shown.
Binary file added src/assets/editor_example_1_more_square.webp
Binary file not shown.
Binary file removed src/assets/editor_example_2.png
Binary file not shown.
Binary file added src/assets/editor_example_2.webp
Binary file not shown.
Binary file removed src/assets/example_schematic.png
Binary file not shown.
Binary file added src/assets/example_schematic.webp
Binary file not shown.
29 changes: 27 additions & 2 deletions src/components/OptimizedImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,36 @@ export function OptimizedImage({
}: OptimizedImageProps) {
const [imageLoading, setImageLoading] = useState(true)

const generateSrcSet = (format: string) => {
const sizes = [320, 640, 960, 1280, 1920]
return sizes
.map((size) => `${src}?format=${format}&w=${size} ${size}w`)
.join(", ")
}

const getSizes = () => {
if (width) {
// If width is specified, use it as a max-width
return `(max-width: ${width}px) 100vw, ${width}px`
}
// Default responsive sizes
return "(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
}

return (
<picture>
<source srcSet={`${src}?format=webp`} type="image/webp" />
<source
type="image/avif"
srcSet={generateSrcSet("avif")}
sizes={getSizes()}
/>
<source
type="image/webp"
srcSet={generateSrcSet("webp")}
sizes={getSizes()}
/>
<img
src={src}
src={`${src}?format=webp&w=${width || 1280}`}
alt={alt}
width={width}
height={height}
Expand Down

0 comments on commit e24002b

Please sign in to comment.