diff --git a/src/Helpers/ImageHelper.php b/src/Helpers/ImageHelper.php index 6d96ee9..4b3ffc7 100644 --- a/src/Helpers/ImageHelper.php +++ b/src/Helpers/ImageHelper.php @@ -280,19 +280,19 @@ public function generateSrcSet(int $attachmentId, array $sizes, string|float|int } /** - * @param array $sizes + * @param array $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(); @@ -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; @@ -353,6 +354,8 @@ protected function generateSizesAttribute(array $sizes): ?string } else { $sizesAttributeParts[] = $width; } + + $prevWidth = $width; } } @@ -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]; } diff --git a/src/Objects/BreakPoint.php b/src/Objects/BreakPoint.php index df12506..db683b6 100644 --- a/src/Objects/BreakPoint.php +++ b/src/Objects/BreakPoint.php @@ -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 @@ -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; }