Skip to content

Commit

Permalink
fix(index): Fix image size on index page
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Jul 5, 2023
1 parent e7b6a87 commit 8d64f70
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
68 changes: 35 additions & 33 deletions src/components/Image.astro
Expand Up @@ -5,59 +5,61 @@ import { getConfiguredImageService, getImage } from "astro:assets";
import type { LocalImageServiceWithPlaceholder } from "../imageService";
export interface Props extends Omit<HTMLAttributes<"img">, "src"> {
src: ImageMetadata;
alt: string;
imageWidth?: number;
imageHeight?: number;
src: ImageMetadata;
alt: string;
imageWidth?: number;
imageHeight?: number;
quality?: number;
}
const originalFormat = await getImage({
src: Astro.props.src,
width: Astro.props.imageWidth,
height: Astro.props.imageHeight,
format: Astro.props.src.format,
src: Astro.props.src,
width: Astro.props.imageWidth,
height: Astro.props.imageHeight,
format: Astro.props.src.format,
});
const formats = ["avif", "webp"];
const sources: Record<string, Awaited<ReturnType<typeof getImage>>> = {};
for (let format of formats) {
sources[format] = await getImage({
src: Astro.props.src,
width: Astro.props.imageWidth,
height: Astro.props.imageHeight,
format: format,
});
sources[format] = await getImage({
src: Astro.props.src,
width: Astro.props.imageWidth,
height: Astro.props.imageHeight,
format: format,
quality: Astro.props.quality,
});
}
const imageService = (await getConfiguredImageService()) as LocalImageServiceWithPlaceholder;
const placeholderURL = await imageService.generatePlaceholder(
Astro.props.src.src,
Astro.props.src.width,
Astro.props.src.height,
Astro.props.src.src,
Astro.props.src.width,
Astro.props.src.height,
);
const { src, imageHeight, imageWidth, ...passedAttributes } = Astro.props;
if (!passedAttributes.width) {
passedAttributes.width = originalFormat.attributes.width;
passedAttributes.width = originalFormat.attributes.width;
}
if (!passedAttributes.height) {
passedAttributes.height = originalFormat.attributes.height;
passedAttributes.height = originalFormat.attributes.height;
}
const { width, height, ...attributes } = originalFormat.attributes;
---

<picture>
{
Object.values(sources).map((source) => (
<source srcset={source.src} type={"image/" + source.options.format} />
))
}
<img
src={originalFormat.src}
style={`background-size: cover;background-image: url(${placeholderURL});image-rendering:auto;`}
onload="
this.removeAttribute('style');
"
{...passedAttributes}
{...attributes}
/>
{
Object.values(sources).map((source) => (
<source srcset={source.src} type={"image/" + source.options.format} />
))
}
<img
src={originalFormat.src}
style={`background-size: cover;background-image: url(${placeholderURL});image-rendering:auto;`}
onload="
this.removeAttribute('style');
"
{...passedAttributes}
{...attributes}
/>
</picture>
Binary file modified src/content/projects/websites/fairedesjeux/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/content/projects/websites/wiki.fairedesjeux/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/pages/index.astro
Expand Up @@ -49,10 +49,11 @@ const featuredProjects = (await getCollection("projects"))
class="h-[180px] w-[377px] rounded-sm object-cover object-top"
src={project.data.indexCover}
alt={project.data.indexCoverAlt ?? ""}
imageWidth={755}
imageWidth={754}
width={377}
height={180}
loading={"eager"}
quality={45}
/>
<h4 class="cover-title">{project.data.title}</h4>
</a>
Expand Down

0 comments on commit 8d64f70

Please sign in to comment.