1.0.14 • Published 4 years ago

svelte-responsive-images v1.0.14

Weekly downloads
21
License
ISC
Repository
github
Last release
4 years ago

Svelte-responsive-images

Svelte-responsive-images is a preprocessor which automates image optimization using sharp.

It parses your img tags, optimizes and replaces src accordingly to the width of the parent element. (External images are not optimized.)

Image component enables lazyloading and serving webp and a fallback jpg with multiple sizes via srcset, and sets a tiny 10x10 placeholder of the source image, while the larger image is loading.

Live Demo, src demo

Install

npm i svelte-responsive-images

Usage

Edit your rollup configuration.

# in rollup.config.js
import { imageOptimizer } from 'svelte-responsive-images'
....
plugins: [
    ImageOptimizer(),
    ....
    ]

Now you can the component, but note that we aren't adding any extensions to the filename.

<script>
	import { Image } from "svelte-responsive-images";
</script>

<Image src="1" />
<Image src="1"
	   w="500px"
	   c="rounded"
	   />

// w=maxwidth, c=class(has to be global)

How does the optimization work?

  • On start time the script looks for images in /public/
  • Performs checks if /public/images/jpg/ or /webp/ exsists if not makes them.
  • After that checks if the optimized files aren't already there and creates optimized versions of images as seen in table below.
  • I went for 70% for quality of jpg/webp. The reason is that I'm serving one step higher resolution as it's cheaper than going up in quality, but still maintaining good filesize and quality.
sourcesizewebpsizejpgsize
Sample.jpg8.37mbSample-1920.webp187kbSample-1920.jpg407kb
Sample-1420.webp118kbSample-1420.jpg246kb
Sample-1220.webp95kbSample-1220.jpg188kb
Sample-1020.webp73kbSample-1020.jpg139kb
Sample-920.webp73kbSample-920.jpg113kb
Sample-720.webp42kbSample-720.jpg76kb
Sample-620.webp33kbSample-620.jpg60kb
Sample-520.webp26kbSample-520.jpg45kb
Sample-420.webp18kbSample-420.jpg32kb
Sample-320.webp12kbSample-320.jpg21kb
Sample-200.webp7kbSample-200.jpg11kb
Sample-100.webp3kbSample-100.jpg4kb
Sample-50.webp908bSample-50.jpg2kb
Sample-25.webp334bSample-25.jpg650b
Sample-10.webp118bSample-10.jpg341b

Component

The component adjusts srcset to the width of the parent container. In the demo example, it's 1050px wide.

   <picture>
       <source type="image/webp"
       srcset="./images/webp/100-50.webp 25w,
       ./images/webp/100-100.webp 50w,
       ./images/webp/100-200.webp 100w,
       ./images/webp/100-320.webp 200w,
       ./images/webp/100-420.webp 320w,
       ./images/webp/100-520.webp 420w,
       ./images/webp/100-620.webp 520w,
       ./images/webp/100-720.webp 620w,
       ./images/webp/100-920.webp 720w,
       ./images/webp/100-1020.webp 920w,
       ./images/webp/100-1220.webp 1020w, ">

       <source srcset="./images/jpg/100-50.jpg 25w,
       ./images/jpg/100-100.jpg 50w,
       ./images/jpg/100-200.jpg 100w,
       ./images/jpg/100-320.jpg 200w,
       ./images/jpg/100-420.jpg 320w,
       ./images/jpg/100-520.jpg 420w,
       ./images/jpg/100-620.jpg 520w,
       ./images/jpg/100-720.jpg 620w,
       ./images/jpg/100-920.jpg 720w,
       ./images/jpg/100-1020.jpg 920w,
       ./images/jpg/100-1220.jpg 1020w, ">

       <img class="main  svelte-i277u2"
	       alt="alt"
	       srcset="./images/jpg/100-10.jpg"
	       onerror="this.src = 'noPhotoFound.png';"
	       loading="lazy">
</picture>
1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago