Skip to content

Commit

Permalink
Merge pull request #12 from offbeatwp/feature/breakpoints
Browse files Browse the repository at this point in the history
Feature/breakpoints
  • Loading branch information
maikelRAOW authored May 28, 2024
2 parents f231a5a + c614704 commit 49d3e14
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/Helpers/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,19 @@ public function generateSrcSet(int $attachmentId, array $sizes, string|float|int
}

/**
* @param array<int, BreakPoint> $sizes
* @param array<int, BreakPoint> $breakpoints
* @return array{sizes: string[]|null[], media_query: string, srcset?: string[]}[]
*/
protected function generateSources(array $sizes, ?string $aspectRatio): array
protected function generateSources(array $breakpoints, ?string $aspectRatio): array
{
$sources = [];
$sourceSizes = [];

foreach ($sizes as $breakpointWidth => $breakpoint) {
foreach ($breakpoints as $breakpointWidth => $breakpoint) {
$source = ['sizes' => []];

$nextBreakpointWidth = $this->getNextKey($sizes, $breakpointWidth);
$nextBreakpoint = $sizes[$nextBreakpointWidth] ?? null;
$nextBreakpointWidth = $this->getNextKey($breakpoints, $breakpointWidth);
$nextBreakpoint = $breakpoints[$nextBreakpointWidth] ?? null;

$sourceSizes[$breakpointWidth] = $breakpoint->getWidth();

Expand Down Expand Up @@ -342,9 +342,10 @@ protected function generateSources(array $sizes, ?string $aspectRatio): array
protected function generateSizesAttribute(array $sizes): ?string
{
$sizesAttributeParts = [];
$prevWidth = null;

foreach ($sizes as $breakpointWidth => $width) {
if ($width) {
if ($width && $width !== $prevWidth) {
$nextBreakpointWidth = $this->getNextKey($sizes, $breakpointWidth);
$nextWidth = $sizes[$nextBreakpointWidth] ?? null;

Expand All @@ -353,6 +354,8 @@ protected function generateSizesAttribute(array $sizes): ?string
} else {
$sizesAttributeParts[] = $width;
}

$prevWidth = $width;
}
}

Expand Down Expand Up @@ -460,7 +463,7 @@ public static function calculateAspectRatio(string|float|int|null $aspectRatio,

$originalImageSize = wp_get_attachment_image_src($attachmentId, 'full');

if (is_array($originalImageSize) && !empty($originalImageSize)) {
if (is_array($originalImageSize) && $originalImageSize) {
return (int)$originalImageSize[1] / (int)$originalImageSize[2];
}

Expand Down
13 changes: 4 additions & 9 deletions src/Objects/BreakPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ final class BreakPoint
{
private int $attachmentId;
private string $width;
private ?string $unit = null;
private string $unit;

public function __construct(int $attachmentId, string $width)
{
$this->attachmentId = $attachmentId;
$this->width = $width;

if (str_ends_with($this->width, 'px')) {
$this->unit = 'px';
} elseif (str_ends_with($this->width, 'vw')) {
$this->unit = 'vw';
}
$this->unit = str_ends_with($this->width, 'vw') ? 'vw' : 'px';
}

public function getAttachmentId(): int
Expand All @@ -30,8 +25,8 @@ public function getWidth(): string
return $this->width;
}

/** @return 'px'|'vw'|null */
public function getUnit(): ?string
/** @return 'px'|'vw' */
public function getUnit(): string
{
return $this->unit;
}
Expand Down

0 comments on commit 49d3e14

Please sign in to comment.