Skip to content

Commit b1e78d9

Browse files
committed
use vw instead of px
1 parent eb0e292 commit b1e78d9

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/BreakpointType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public function srcset(int $screenSize, string $src): string
1313
return "{$src} {$screenSize}w";
1414
}
1515

16-
public function size(int $screenSize): string
16+
public function size(int $screenSize, int $maxWidth): string
1717
{
18-
$width = $this->width();
19-
return "($width: {$screenSize}px) {$screenSize}px";
18+
$vw = floor(($screenSize / $maxWidth) * 100);
19+
return "({$this->width()}: {$screenSize}px) {$vw}vw";
2020
}
2121

2222
public function width(): string

src/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function attributes(string|Responsive $src, $alt, $width = null, $
6464

6565
if ($src instanceof Responsive) {
6666
$attributes[] = sprintf('srcset="%s"', $src->getSrcset($image));
67-
$attributes[] = sprintf('sizes="%s"', $src->getSizes());
67+
$attributes[] = sprintf('sizes="%s"', $src->getSizes($image));
6868
}
6969

7070
return implode(" ", $attributes);

src/Responsive.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ public function add(string $screenSize, BreakpointType $type, ?int $width = null
3131

3232
public function getSrcset(Image $image): string
3333
{
34-
$originalWidth = $image->getWidth();
34+
$img = clone $image;
35+
$originalWidth = $img->getWidth();
3536
$hasLargeImage = false;
36-
$fallback = $image->getSrc() . ' ' . $originalWidth . 'w';
37+
$fallback = $img->getSrc() . ' ' . $originalWidth . 'w';
3738
$srcset = [];
3839
foreach ($this->_srcset as $src) {
3940
/** @var BreakpointType $type */
4041
$type = $src[1];
41-
$srcset[] = $type->srcset($src[0], $image->setWidth($src[2])->setHeight($src[3])->getSrc());
42+
$srcset[] = $type->srcset($src[0], $img->setWidth($src[2])->setHeight($src[3])->getSrc());
4243

4344
if ($src[2] > $originalWidth) {
4445
$hasLargeImage = true;
@@ -50,13 +51,20 @@ public function getSrcset(Image $image): string
5051
return implode(", ", $srcset);
5152
}
5253

53-
public function getSizes(): string
54+
public function getSizes(Image $image): string
5455
{
56+
$maxSize = $image->getWidth();
57+
foreach ($this->_srcset as $src) {
58+
if ($src[2] > $maxSize) {
59+
$maxSize = $src[2];
60+
}
61+
}
62+
5563
$sizes = [];
5664
foreach ($this->_srcset as $src) {
5765
/** @var BreakpointType $type */
5866
$type = $src[1];
59-
$sizes[] = $type->size($src[0]);
67+
$sizes[] = $type->size($src[0], $maxSize);
6068
}
6169
$sizes[] = '100vw';
6270
return implode(", ", $sizes);

tests/ResponsiveImageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testImageConstructor()
1515

1616
$image = Image::attributes($responsive, 'Test Image', 100, 200, 'png', 'auto', 'sync');
1717

18-
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" width="100" height="200" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1200xnull?format=png 1000w" sizes="(max-width: 400px) 400px, (min-width: 1000px) 1000px, 100vw"', $image);
18+
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" width="100" height="200" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1200xnull?format=png 1000w" sizes="(max-width: 400px) 33vw, (min-width: 1000px) 83vw, 100vw"', $image);
1919
}
2020

2121
public function testSingleResponsiveImageWithRawOriginalSizeFallback()
@@ -25,6 +25,6 @@ public function testSingleResponsiveImageWithRawOriginalSizeFallback()
2525

2626
$image = Image::attributes($responsive, 'Test Image', 1000, 800, 'png', 'auto', 'sync');
2727

28-
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png" alt="Test Image" loading="eager" decoding="sync" width="1000" height="800" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png 1000w" sizes="(max-width: 400px) 400px, 100vw"', $image);
28+
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png" alt="Test Image" loading="eager" decoding="sync" width="1000" height="800" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png 1000w" sizes="(max-width: 400px) 40vw, 100vw"', $image);
2929
}
3030
}

0 commit comments

Comments
 (0)