summaryrefslogtreecommitdiff
path: root/src/components/common/Image.astro
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-07-21 21:56:55 +0300
committerDawid Rycerz <dawid@rycerz.xyz>2025-07-21 21:56:55 +0300
commitc735556726e75428550a3d28a2cf58e2c8490b7d (patch)
treefd0ae29d1636b825abeedff6b99d3376bb383135 /src/components/common/Image.astro
Initial template
Diffstat (limited to 'src/components/common/Image.astro')
-rw-r--r--src/components/common/Image.astro61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/components/common/Image.astro b/src/components/common/Image.astro
new file mode 100644
index 0000000..d113b68
--- /dev/null
+++ b/src/components/common/Image.astro
@@ -0,0 +1,61 @@
+---
+import type { HTMLAttributes } from 'astro/types';
+import { findImage } from '~/utils/images';
+import {
+ getImagesOptimized,
+ astroAssetsOptimizer,
+ unpicOptimizer,
+ isUnpicCompatible,
+ type ImageProps,
+} from '~/utils/images-optimization';
+
+type Props = ImageProps;
+type ImageType = {
+ src: string;
+ attributes: HTMLAttributes<'img'>;
+};
+
+const props = Astro.props;
+
+if (props.alt === undefined || props.alt === null) {
+ throw new Error();
+}
+
+if (typeof props.width === 'string') {
+ props.width = parseInt(props.width);
+}
+
+if (typeof props.height === 'string') {
+ props.height = parseInt(props.height);
+}
+
+if (!props.loading) {
+ props.loading = 'lazy';
+}
+
+if (!props.decoding) {
+ props.decoding = 'async';
+}
+
+const _image = await findImage(props.src);
+
+let image: ImageType | undefined = undefined;
+
+if (
+ typeof _image === 'string' &&
+ (_image.startsWith('http://') || _image.startsWith('https://')) &&
+ isUnpicCompatible(_image)
+) {
+ image = await getImagesOptimized(_image, props, unpicOptimizer);
+} else if (_image) {
+ image = await getImagesOptimized(_image, props, astroAssetsOptimizer);
+}
+---
+
+{
+ !image ? (
+ <Fragment />
+ ) : (
+ <img src={image.src} crossorigin="anonymous" referrerpolicy="no-referrer" {...image.attributes} />
+ )
+}