Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Modifiers/ContainModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private function contain(FrameInterface $frame, SizeInterface $resize, ColorInte
'no_rotate' => true,
]);

if ($resized->bands < 3) {
// Grayscale -> RGB
$resized = $resized->colourspace('srgb');
}

if (!$resized->hasAlpha()) {
$resized = $resized->bandjoin_const(255);
}
Expand Down
17 changes: 11 additions & 6 deletions src/Modifiers/CropModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ private function background(SizeInterface $resizeTo, ImageInterface $image): Vip
$bgColor->channel(Blue::class)->value(),
];

$originalImage = $image->core()->native();
if ($originalImage->bands === 1) {
$imageNative = $image->core()->native();
if ($imageNative->bands < 3) {
// Grayscale -> RGB
$originalImage = $originalImage->colourspace('srgb');
$imageNative = $imageNative->colourspace('srgb');
}

// original image and background must have the same number of bands
if ($originalImage->hasAlpha()) {
if ($imageNative->hasAlpha()) {
$bands[] = $bgColor->channel(Alpha::class)->value();
}

return VipsImage::black(1, 1)
->add($bgColor->channel(Red::class)->value())
->cast($originalImage->format)
->cast($imageNative->format)
->embed(0, 0, $resizeTo->width(), $resizeTo->height(), ['extend' => Extend::COPY])
->copy(['interpretation' => $originalImage->interpretation])
->copy(['interpretation' => $imageNative->interpretation])
->bandjoin($bands);
}

Expand Down Expand Up @@ -136,6 +136,11 @@ private function cropFrame(
);

if ($crop->width() > $originalSize->width() || $cropped->height < $crop->height()) {
if ($cropped->bands < 3) {
// Grayscale -> RGB
$cropped = $cropped->colourspace('srgb');
}

$cropped = $background->insert(
$cropped,
max($offset_x * -1, 0),
Expand Down
9 changes: 5 additions & 4 deletions src/Modifiers/PadModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ private function pad(FrameInterface $frame, SizeInterface $resize, ColorInterfac
'no_rotate' => true,
]);

if ($resized->bands < 3) {
// Grayscale -> RGB
$resized = $resized->colourspace('srgb');
}

if (!$resized->hasAlpha()) {
if ($resized->bands === 1) {
// Grayscale -> RGB
$resized = $resized->colourspace('srgb');
}
$resized = $resized->bandjoin_const(255);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Modifiers/ContainModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ public function testModifyContainAnimated(): void
$this->assertColor(255, 255, 255, 0, $frame->toImage(new Driver())->pickColor(0, 0));
}
}

public function testModifyContainGrayscale(): void
{
$image = $this->readTestImage('grayscale.png');
$this->assertEquals(150, $image->width());
$this->assertEquals(200, $image->height());
$image->modify(new ContainModifier(200, 200, 'transparent', 'top'));
$this->assertEquals(200, $image->width());
$this->assertEquals(200, $image->height());
$this->assertColor(255, 255, 255, 0, $image->pickColor(0, 0));
$this->assertColor(0, 0, 0, 255, $image->pickColor(50, 0));
}

public function testModifyContainGrayscaleAlpha(): void
{
$image = $this->readTestImage('grayscale-alpha.png');
$this->assertEquals(256, $image->width());
$this->assertEquals(256, $image->height());
$image->modify(new ContainModifier(258, 256, 'transparent', 'top'));
$this->assertEquals(258, $image->width());
$this->assertEquals(256, $image->height());
$this->assertColor(255, 255, 255, 0, $image->pickColor(0, 0));
$this->assertColor(0, 0, 0, 128, $image->pickColor(1, 0));
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Modifiers/CropModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ public function testCropGrayscale(): void
// Ensure the image is encodable
$image->encode();
}

public function testCropGrayscaleAlpha(): void
{
$image = $this->readTestImage('grayscale-alpha.png');
$image->modify(new CropModifier(258, 258, 0, 0, 'ff0000', 'center'));
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 0));
$this->assertColor(0, 0, 0, 128, $image->pickColor(1, 1));
$this->assertColor(255, 255, 255, 128, $image->pickColor(256, 1));

// Ensure the image is encodable
$image->encode();
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Modifiers/PadModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ public function testModifyGrayscale(): void
$this->assertColor(255, 0, 0, 255, $image->pickColor(199, 0));
$this->assertColor(255, 0, 0, 255, $image->pickColor(199, 199));
}

public function testModifyGrayscaleAlpha(): void
{
$image = $this->readTestImage('grayscale-alpha.png');
$this->assertEquals(256, $image->width());
$this->assertEquals(256, $image->height());
$image->modify(new PadModifier(258, 258, 'f00'));
$this->assertEquals(258, $image->width());
$this->assertEquals(258, $image->height());
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 0));
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 257));
$this->assertColor(255, 0, 0, 255, $image->pickColor(257, 0));
$this->assertColor(255, 0, 0, 255, $image->pickColor(257, 257));
}
}
Binary file added tests/resources/grayscale-alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.