Skip to content

Commit

Permalink
🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
akeylimepie committed Sep 7, 2022
1 parent 510de86 commit a068d19
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class Picture
protected $image;
protected $width = null;
protected $height = null;
protected $ratio = null;

protected $breakpoints = [];

protected $attributes = [];

protected $mode;

public function __construct(Image $image, ?int $width = null, ?int $height = null)
{
$this->image = $image;
Expand All @@ -23,6 +26,10 @@ public function __construct(Image $image, ?int $width = null, ?int $height = nul
$this->width = $image->dimensions->width;
$this->height = $image->dimensions->height;
}

if ($this->width && $this->height) {
$this->ratio = $this->width / $this->height;
}
}

public function __toString()
Expand Down Expand Up @@ -69,6 +76,15 @@ public function class(string $class): self
public function responsive(array $breakpoints = [400 => 400, 700 => 700]): self
{
$this->breakpoints = $breakpoints;
$this->mode = 'responsive';

return $this;
}

public function adaptive(array $breakpoints = [400 => 400, 700 => 700]): self
{
$this->breakpoints = $breakpoints;
$this->mode = 'adaptive';

return $this;
}
Expand All @@ -82,7 +98,7 @@ protected function calculateBreakpoints(): array
}

foreach ($this->breakpoints as $breakpoint => $width) {
if ($this->width > $breakpoint) {
if ($this->width > $width) {
$result[$breakpoint] = $width;
}
}
Expand Down Expand Up @@ -119,13 +135,17 @@ protected function renderViewportAware(array $breakpoints): string
{
$html = '';

$height = $this->height;

foreach (array_merge(ImageProxy::getAvailableFormats(), [null]) as $format) {
$srcset = [];
$sizes = [];

foreach ($breakpoints as $breakpoint => $width) {
if ($this->mode === 'adaptive') {
$height = $width / $this->ratio;
} else {
$height = $this->height;
}

$srcset[] = $this->image->getUrl($width, $height, 1, $format) . sprintf(' %dw', $width);
$srcset[] = $this->image->getUrl($width, $height, 2, $format) . sprintf(' %dw', $width * 2);

Expand All @@ -142,18 +162,24 @@ protected function renderViewportAware(array $breakpoints): string
$sizes[] = sprintf('%dpx', $this->width);

if (null === $format) {
$html .= '<img src="' . explode(' ', $srcset[0])[0] . '" srcset="' . implode(', ', $srcset) . '" sizes="' . implode(
$html .= '<img src="' . explode(' ', $srcset[0])[0] . '" srcset="' . implode(
', ',
$srcset
) . '" sizes="' . implode(
', ',
$sizes
) . '"';

if ($height) {
$html .= ' height="' . $height . '"';
if ($this->height && $this->mode === 'responsive') {
$html .= ' height="' . $this->height . '"';
}

$html .= $this->renderAttributes() . '>';
} else {
$html .= '<source type="image/'.$format->value.'" srcset="' . implode(', ', $srcset) . '" sizes="' . implode(', ', $sizes) . '">';
$html .= '<source type="image/' . $format->value . '" srcset="' . implode(
', ',
$srcset
) . '" sizes="' . implode(', ', $sizes) . '">';
}
}

Expand All @@ -174,6 +200,10 @@ protected function renderDPRAware(): string
if (null === $format) {
$html .= '<img src="' . explode(' ', $srcset[0])[0] . '" srcset="' . implode(', ', $srcset) . '"';

if ($this->width) {
$html .= ' width="' . $this->width . '"';
}

if ($this->height) {
$html .= ' height="' . $this->height . '"';
}
Expand Down

0 comments on commit a068d19

Please sign in to comment.