Skip to content

Commit

Permalink
fix: crop srcset images if the source image has the same width as src…
Browse files Browse the repository at this point in the history
…set value
  • Loading branch information
carlalexander committed Nov 30, 2022
1 parent bd7c682 commit b55ab3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,15 @@ public function rewriteImageSrcset($sources, $size, $imageSrc, $imageMetadata, $

list($width, $height) = $this->parseImageDimensionsFromFilename($source['url']);

$cropped = $height && (int) $source['value'] === $width;

if (!$height && !$width) {
$width = $source['value'];
}

$url = is_numeric($attachmentId) ? wp_get_attachment_url($attachmentId) : $this->getOriginalImageUrl($source['url']);

$source['url'] = $this->generateImageUrl($url, $height, $width);
$source['url'] = $this->generateImageUrl($url, $height, $width, $cropped);

return $source;
})->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,30 @@ public function testRewriteImageSrcsetFallsBackToValueAttributeIfUnableToParseIm
$this->assertSame($expectedSources, (new ContentDeliveryNetworkImageProcessingSubscriber($this->getImageSizes(), true, 'https://assets.com/uploads'))->rewriteImageSrcset($actualSources, null, null, null, 42));
}

public function testRewriteImageSrcsetParsesDimensionsFromUrlAttribute()
public function testRewriteImageSrcsetParsesDimensionsFromUrlAttributeAndCropsWithSameWidthAsValue()
{
$actualSources = [
['value' => 42, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image-150x150.jpg'],
['value' => 150, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image-150x120.jpg'],
];
$expectedSources = [
['value' => 42, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image.jpg?height=150&width=150'],
['value' => 150, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image.jpg?height=120&width=150&cropped'],
];

$wp_get_attachment_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_url');
$wp_get_attachment_url->expects($this->once())
->with($this->identicalTo(42))
->willReturn('https://assets.com/uploads/image.jpg');

$this->assertSame($expectedSources, (new ContentDeliveryNetworkImageProcessingSubscriber($this->getImageSizes(), true, 'https://assets.com/uploads'))->rewriteImageSrcset($actualSources, null, null, null, 42));
}

public function testRewriteImageSrcsetParsesDimensionsFromUrlAttributeAndDoesntCropWithDifferentWidthFromValue()
{
$actualSources = [
['value' => 42, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image-150x120.jpg'],
];
$expectedSources = [
['value' => 42, 'descriptor' => 'w', 'url' => 'https://assets.com/uploads/image.jpg?height=120&width=150'],
];

$wp_get_attachment_url = $this->getFunctionMock($this->getNamespace(ContentDeliveryNetworkImageProcessingSubscriber::class), 'wp_get_attachment_url');
Expand Down

0 comments on commit b55ab3d

Please sign in to comment.