22
33> Elegant responsive images for SvelteKit.
44
5- Automatically transform local images into multiple widths and next-gen formats, then render its
6- associated HTML into your SvelteKit project.
5+ Automatically transform local images into multiple widths and next-gen formats, then render a
6+ minimally invasive HTML representation into your SvelteKit project.
77
88Demo: https://zerodevx.github.io/svelte-img/
99
@@ -41,7 +41,7 @@ import cat from '$lib/assets/cat.jpg?run'
4141import Img from ' @zerodevx/svelte-img'
4242 </script >
4343
44- <Img class =" my classes " src ={cat} alt =" Cute cat" />
44+ <Img class =" cool cats " src ={cat} alt =" Cute cat" />
4545```
4646
4747The image component renders into:
@@ -57,7 +57,7 @@ The image component renders into:
5757 srcset =" path/to/webp-480 480w, path/to/webp-1024 1024w, path/to/webp-1920 1920w"
5858 />
5959 <img
60- class =" my classes "
60+ class =" cool cats "
6161 src =" path/to/jpg/480"
6262 srcset =" path/to/jpg-480 480w, path/to/jpg-1024 1024w, path/to/jpg-1920 1920w"
6363 loading =" lazy"
@@ -68,6 +68,138 @@ The image component renders into:
6868</picture >
6969```
7070
71+ ## Features
72+
73+ #### Change default widths/formats
74+
75+ Local image manipulation is delegated to the excellent
76+ [ vite-imagetools] ( https://github.com/JonasKruckenberg/imagetools ) with a custom ` ?run ` directive. By
77+ default, this generates 10 variants of every image - ` avif/webp/jpg ` formats at ` 480/1024/1920 `
78+ widths; and a ` 16px webp/base64 ` low-quality image placeholder (LQIP).
79+
80+ To change this globally, edit your ` vite.config.js ` :
81+
82+ ``` js
83+ import { sveltekit } from ' @sveltejs/kit/vite'
84+ import { imagetools , set } from ' @zerodevx/svelte-img/vite'
85+
86+ /** @type {import('vite').UserConfig} */
87+ const config = {
88+ plugins: [
89+ sveltekit (),
90+ imagetools ({
91+ // By default, directives are `?width=480;1024;1920&format=avif;webp;jpg`
92+ defaultDirectives : (url ) => set (url, ' ?width=768;1920&format=avif;jpg' )
93+ // Now this generates 5 variants - `avif/jpg` formats at `768/1024` + LQIP
94+ })
95+ ]
96+ }
97+
98+ export default config
99+ ```
100+
101+ #### On a per-image basis
102+
103+ Widths/formats can be applied to a particular image. From your ` .svelte ` file:
104+
105+ <!-- prettier-ignore -->
106+ ``` html
107+ <script >
108+ import src from ' $lib/a/cat.jpg?width=768;1920&format=avif;jpg&run'
109+ import Img from ' @zerodevx/svelte-img'
110+ </script >
111+
112+ <Img {src} alt =" cat" >
113+ ```
114+
115+ #### Change LQIP width
116+
117+ By default, LQIPs are 16px in width and set to ` cover ` the full image dimension. Increase for a
118+ higher quality LQIP at the expense of a larger ` base64 ` , or set to 1px for a dominant single-colour
119+ background.
120+
121+ <!-- prettier-ignore -->
122+ ``` html
123+ <script >
124+ import src from ' $lib/a/cat.jpg?lqip=1&run'
125+ import Img from ' @zerodevx/svelte-img'
126+ </script >
127+
128+ <!-- Should render a dominant single-colour background -->
129+ <Img {src} alt =" cat" >
130+ ```
131+
132+ #### Other transformations
133+
134+ The full [ repertoire] ( https://github.com/JonasKruckenberg/imagetools/blob/main/docs/directives.md )
135+ of transformation directives offered by
136+ [ vite-imagetools] ( https://github.com/JonasKruckenberg/imagetools ) can be used.
137+
138+ <!-- prettier-ignore -->
139+ ``` html
140+ <script >
141+ import src from ' $lib/a/cat.jpg?&height=600&fit=cover&normalize&run'
142+ import Img from ' @zerodevx/svelte-img'
143+ </script >
144+
145+ <Img {src} alt =" cat" >
146+ ```
147+
148+ #### Remote images from an API
149+
150+ The ` svelte-img ` component can be used on its own and accepts ` src ` as an array of image objects
151+ like so:
152+
153+ <!-- prettier-ignore -->
154+ ``` js
155+ [
156+ { format: ' avif' , src: ' https://x.com/path/to/avif-480' , width: 480 },
157+ { format: ' webp' , src: ' https://x.com//path/to/webp-480' , width: 480 },
158+ { format: ' jpg' , src: ' https://x.com//path/to/jpg-480' , width: 480 },
159+ { format: ' avif' , src: ' https://x.com//path/to/avif-1024' , width: 1024 },
160+ ... // all other variants
161+ { base64: ' data:image/webp;base64,XXX' } // an optional LQIP with `base64` key
162+ ]
163+ ```
164+
165+ The order doesn't matter; ` svelte-img ` internally sorts out the rendering priority based on:
166+
167+ ``` js
168+ // Highest to lowest
169+ const priority = [' heic' , ' heif' , ' avif' , ' webp' , ' jpeg' , ' jpg' , ' png' , ' gif' , ' tiff' ]
170+ ```
171+
172+ #### A better blur
173+
174+ Natively, browsers do already apply _ some_ blur when displaying low resolution images. That's enough
175+ for me, but you can apply your own using CSS.
176+
177+ <!-- prettier-ignore -->
178+ ``` html
179+ <script >
180+ import src from ' $lib/a/cat.jpg?run'
181+ import Img from ' @zerodevx/svelte-img'
182+ </script >
183+
184+ <Img {src} alt =" cat" >
185+
186+ <style >
187+ :global(img )::after {
188+ content : ' ' ;
189+ position : absolute ;
190+ inset : 0 ;
191+ backdrop-filter : blur (20px );
192+ pointer-events : none ;
193+ }
194+ </style >
195+ ```
196+
197+ ## To do
198+
199+ - [ ] Add type annotations
200+ - [ ] Add tests
201+ - [ ] Document more features
202+
71203## License
72204
73205ISC
0 commit comments