coyotte508 HF staff commited on
Commit
012a5bd
1 Parent(s): 957f69f

🔍 Better image size

Browse files
src/lib/components/Picture.svelte CHANGED
@@ -2,17 +2,71 @@
2
  import type { Picture } from '$lib/types/Picture';
3
 
4
  export let picture: Picture | undefined;
5
- export let minStorage = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  </script>
7
 
8
  {#if picture}
9
  <img
10
  alt={picture.name}
11
  srcset={picture.storage
12
- .slice(minStorage)
13
  .map((format) => `/photos/raw/${format._id} ${format.width}w`)
14
  .join(', ')}
 
 
 
15
  {...$$restProps}
 
 
 
16
  on:click
17
  on:load
18
  />
 
2
  import type { Picture } from '$lib/types/Picture';
3
 
4
  export let picture: Picture | undefined;
5
+
6
+ let className = '';
7
+ export { className as class };
8
+ export let style = '';
9
+
10
+ let matchedWidth: number | null = null;
11
+ let matchedHeight: number | null = null;
12
+
13
+ $: if (/(\s|^)w-(\d+)(\s|$)/.test(className)) {
14
+ matchedWidth = parseInt(className.match(/(\s|^)w-(\d+)(\s|$)/)![2]) * 4;
15
+ } else {
16
+ matchedWidth = null;
17
+ }
18
+
19
+ $: if (/(\s|^)h-(\d+)(\s|$)/.test(className)) {
20
+ matchedHeight = parseInt(className.match(/(\s|^)h-(\d+)(\s|$)/)![2]) * 4;
21
+ } else {
22
+ matchedHeight = null;
23
+ }
24
+
25
+ let computedWidth: number | null = null;
26
+ let computedHeight: number | null = null;
27
+
28
+ $: {
29
+ if (matchedWidth !== null) {
30
+ computedWidth = null;
31
+ } else if (matchedHeight !== null && picture) {
32
+ computedWidth = Math.round(
33
+ (matchedHeight / picture.storage[0].height) * picture.storage[0].width
34
+ );
35
+ } else {
36
+ computedWidth = null;
37
+ }
38
+ }
39
+
40
+ $: {
41
+ if (matchedHeight !== null) {
42
+ computedHeight = null;
43
+ } else if (matchedWidth !== null && picture) {
44
+ computedHeight = Math.round(
45
+ (matchedWidth / picture.storage[0].width) * picture.storage[0].height
46
+ );
47
+ } else {
48
+ computedHeight = null;
49
+ }
50
+ }
51
+
52
+ $: computedStyle = `${computedWidth !== null ? `width: ${computedWidth}px;` : ''} ${
53
+ computedHeight !== null ? `height: ${computedHeight}px;` : ''
54
+ } ${style}`;
55
  </script>
56
 
57
  {#if picture}
58
  <img
59
  alt={picture.name}
60
  srcset={picture.storage
 
61
  .map((format) => `/photos/raw/${format._id} ${format.width}w`)
62
  .join(', ')}
63
+ sizes={matchedWidth ?? computedWidth !== null ? `${matchedWidth ?? computedWidth}px` : `100vw`}
64
+ class={className}
65
+ style={computedStyle}
66
  {...$$restProps}
67
+ on:keydown
68
+ on:keypress
69
+ on:keyup
70
  on:click
71
  on:load
72
  />
src/lib/server/photo.ts CHANGED
@@ -26,7 +26,7 @@ export async function generatePicture(
26
  });
27
  }
28
 
29
- for (const size of [2048, 1024, 512]) {
30
  if (width > size || height > size) {
31
  const buffer = await image
32
  .resize(width > height ? { width: size } : { height: size })
 
26
  });
27
  }
28
 
29
+ for (const size of [2048, 1024, 512, 256, 128]) {
30
  if (width > size || height > size) {
31
  const buffer = await image
32
  .resize(width > height ? { width: size } : { height: size })
src/routes/+page.svelte CHANGED
@@ -91,6 +91,7 @@
91
  picture={pic}
92
  class="rounded-3xl object-contain"
93
  style="max-width: 100%; max-height: 100%"
 
94
  />
95
  </div>
96
  {/each}
@@ -101,6 +102,7 @@
101
  <section class="h-xl rounded-3xl bg-oxford overflow-hidden flex mb-12">
102
  <Picture
103
  class="w-2/6 h-full object-cover"
 
104
  picture={pictures.find((p) => p._id === pageData.pictures['e-shop'])}
105
  />
106
  <div class="w-4/6 h-full px-6 py-12 text-white flex flex-col box-border">
 
91
  picture={pic}
92
  class="rounded-3xl object-contain"
93
  style="max-width: 100%; max-height: 100%"
94
+ sizes="(min-width: 650px) 50vw, 95vw"
95
  />
96
  </div>
97
  {/each}
 
102
  <section class="h-xl rounded-3xl bg-oxford overflow-hidden flex mb-12">
103
  <Picture
104
  class="w-2/6 h-full object-cover"
105
+ sizes="(min-width: 1200px) 400px, 33vw"
106
  picture={pictures.find((p) => p._id === pageData.pictures['e-shop'])}
107
  />
108
  <div class="w-4/6 h-full px-6 py-12 text-white flex flex-col box-border">
src/routes/admin/photos/+page.svelte CHANGED
@@ -13,7 +13,7 @@
13
  {#each data.photos as photo}
14
  <div class="flex flex-col text-center">
15
  <a href="/admin/photos/{photo._id}" class="flex flex-col items-center">
16
- <Picture picture={photo} class="h-36 block" style="object-fit: scale-down;" />
17
  <span>{photo.name}</span>
18
  </a>
19
  </div>
 
13
  {#each data.photos as photo}
14
  <div class="flex flex-col text-center">
15
  <a href="/admin/photos/{photo._id}" class="flex flex-col items-center">
16
+ <Picture picture={photo} class="h-36 block" />
17
  <span>{photo.name}</span>
18
  </a>
19
  </div>
src/routes/admin/produits/+page.svelte CHANGED
@@ -13,7 +13,7 @@
13
  {#each data.products as product}
14
  <div class="flex flex-col text-center">
15
  <a href="/admin/produits/{product._id}" class="flex flex-col items-center">
16
- <Picture picture={product.photos[0]} class="h-36 block" style="object-fit: scale-down;" />
17
  <span class="mt-2">{product.name}</span>
18
  </a>
19
  </div>
 
13
  {#each data.products as product}
14
  <div class="flex flex-col text-center">
15
  <a href="/admin/produits/{product._id}" class="flex flex-col items-center">
16
+ <Picture picture={product.photos[0]} class="h-36 block" />
17
  <span class="mt-2">{product.name}</span>
18
  </a>
19
  </div>
src/routes/tissus-et-finitions/+page.svelte CHANGED
@@ -19,7 +19,7 @@
19
  data-title={picture.name}
20
  >
21
  <Picture
22
- minStorage={1}
23
  {picture}
24
  class="max-w-full {picture.loaded ? 'loaded' : ''}"
25
  loading="lazy"
 
19
  data-title={picture.name}
20
  >
21
  <Picture
22
+ sizes="(min-width: 1024px) 33vw, (min-width: 675px) 50vw, 100vw"
23
  {picture}
24
  class="max-w-full {picture.loaded ? 'loaded' : ''}"
25
  loading="lazy"
src/routes/vente/+page.svelte CHANGED
@@ -16,6 +16,7 @@
16
  <div class="h-full-without-banner w-full relative">
17
  <Picture
18
  picture={pictures.find((p) => p._id === pageData.pictures.background)}
 
19
  class="h-full w-full object-cover absolute top-0 bottom-0 left-0 right-0 bg-brunswick text-white"
20
  style="z-index: -1"
21
  />
@@ -45,7 +46,7 @@
45
  {/if}
46
  <Picture
47
  picture={product.photos[0]}
48
- minStorage={1}
49
  class="picture mx-auto max-w-full h-full hover-zoom rounded"
50
  />
51
  </div>
@@ -69,7 +70,7 @@
69
  <div class="overflow-hidden rounded relative">
70
  <Picture
71
  picture={product.photos[0]}
72
- minStorage={1}
73
  class="picture mx-auto max-w-full h-full hover-zoom rounded"
74
  />
75
  </div>
 
16
  <div class="h-full-without-banner w-full relative">
17
  <Picture
18
  picture={pictures.find((p) => p._id === pageData.pictures.background)}
19
+ sizes="100vw"
20
  class="h-full w-full object-cover absolute top-0 bottom-0 left-0 right-0 bg-brunswick text-white"
21
  style="z-index: -1"
22
  />
 
46
  {/if}
47
  <Picture
48
  picture={product.photos[0]}
49
+ sizes="(min-width: 1024px) 33vw, (min-width: 675px) 50vw, 100vw"
50
  class="picture mx-auto max-w-full h-full hover-zoom rounded"
51
  />
52
  </div>
 
70
  <div class="overflow-hidden rounded relative">
71
  <Picture
72
  picture={product.photos[0]}
73
+ sizes="(min-width: 1024px) 33vw, (min-width: 675px) 50vw, 100vw"
74
  class="picture mx-auto max-w-full h-full hover-zoom rounded"
75
  />
76
  </div>
src/routes/vente/[id]/+page.svelte CHANGED
@@ -24,6 +24,7 @@
24
  <a href="/photos/raw/{product.photos[photoIndex].storage[0]._id}" target="_blank">
25
  <Picture
26
  picture={product.photos[photoIndex]}
 
27
  title="Cliquez pour voir la photo entière"
28
  class="max-w-full max-h-md lg:max-h-xl rounded-3xl"
29
  />
 
24
  <a href="/photos/raw/{product.photos[photoIndex].storage[0]._id}" target="_blank">
25
  <Picture
26
  picture={product.photos[photoIndex]}
27
+ sizes="(min-width: 1152px) 576px, (max-width: 1024px) 100vw, 50vw"
28
  title="Cliquez pour voir la photo entière"
29
  class="max-w-full max-h-md lg:max-h-xl rounded-3xl"
30
  />