Skip to content

Commit ad5e532

Browse files
authored
Merge pull request #87 from earthiverse/develop
Add support for Grayscale+Alpha PNGs
2 parents 3693d8f + 89aee51 commit ad5e532

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

src/Modifiers/ContainModifier.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private function contain(FrameInterface $frame, SizeInterface $resize, ColorInte
5757
'no_rotate' => true,
5858
]);
5959

60+
if ($resized->bands < 3) {
61+
// Grayscale -> RGB
62+
$resized = $resized->colourspace('srgb');
63+
}
64+
6065
if (!$resized->hasAlpha()) {
6166
$resized = $resized->bandjoin_const(255);
6267
}

src/Modifiers/CropModifier.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,22 @@ private function background(SizeInterface $resizeTo, ImageInterface $image): Vip
7676
$bgColor->channel(Blue::class)->value(),
7777
];
7878

79-
$originalImage = $image->core()->native();
80-
if ($originalImage->bands === 1) {
79+
$imageNative = $image->core()->native();
80+
if ($imageNative->bands < 3) {
8181
// Grayscale -> RGB
82-
$originalImage = $originalImage->colourspace('srgb');
82+
$imageNative = $imageNative->colourspace('srgb');
8383
}
8484

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

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

@@ -136,6 +136,11 @@ private function cropFrame(
136136
);
137137

138138
if ($crop->width() > $originalSize->width() || $cropped->height < $crop->height()) {
139+
if ($cropped->bands < 3) {
140+
// Grayscale -> RGB
141+
$cropped = $cropped->colourspace('srgb');
142+
}
143+
139144
$cropped = $background->insert(
140145
$cropped,
141146
max($offset_x * -1, 0),

src/Modifiers/PadModifier.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ private function pad(FrameInterface $frame, SizeInterface $resize, ColorInterfac
5555
'no_rotate' => true,
5656
]);
5757

58+
if ($resized->bands < 3) {
59+
// Grayscale -> RGB
60+
$resized = $resized->colourspace('srgb');
61+
}
62+
5863
if (!$resized->hasAlpha()) {
59-
if ($resized->bands === 1) {
60-
// Grayscale -> RGB
61-
$resized = $resized->colourspace('srgb');
62-
}
6364
$resized = $resized->bandjoin_const(255);
6465
}
6566

tests/Unit/Modifiers/ContainModifierTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@ public function testModifyContainAnimated(): void
6565
$this->assertColor(255, 255, 255, 0, $frame->toImage(new Driver())->pickColor(0, 0));
6666
}
6767
}
68+
69+
public function testModifyContainGrayscale(): void
70+
{
71+
$image = $this->readTestImage('grayscale.png');
72+
$this->assertEquals(150, $image->width());
73+
$this->assertEquals(200, $image->height());
74+
$image->modify(new ContainModifier(200, 200, 'transparent', 'top'));
75+
$this->assertEquals(200, $image->width());
76+
$this->assertEquals(200, $image->height());
77+
$this->assertColor(255, 255, 255, 0, $image->pickColor(0, 0));
78+
$this->assertColor(0, 0, 0, 255, $image->pickColor(50, 0));
79+
}
80+
81+
public function testModifyContainGrayscaleAlpha(): void
82+
{
83+
$image = $this->readTestImage('grayscale-alpha.png');
84+
$this->assertEquals(256, $image->width());
85+
$this->assertEquals(256, $image->height());
86+
$image->modify(new ContainModifier(258, 256, 'transparent', 'top'));
87+
$this->assertEquals(258, $image->width());
88+
$this->assertEquals(256, $image->height());
89+
$this->assertColor(255, 255, 255, 0, $image->pickColor(0, 0));
90+
$this->assertColor(0, 0, 0, 128, $image->pickColor(1, 0));
91+
}
6892
}

tests/Unit/Modifiers/CropModifierTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,16 @@ public function testCropGrayscale(): void
100100
// Ensure the image is encodable
101101
$image->encode();
102102
}
103+
104+
public function testCropGrayscaleAlpha(): void
105+
{
106+
$image = $this->readTestImage('grayscale-alpha.png');
107+
$image->modify(new CropModifier(258, 258, 0, 0, 'ff0000', 'center'));
108+
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 0));
109+
$this->assertColor(0, 0, 0, 128, $image->pickColor(1, 1));
110+
$this->assertColor(255, 255, 255, 128, $image->pickColor(256, 1));
111+
112+
// Ensure the image is encodable
113+
$image->encode();
114+
}
103115
}

tests/Unit/Modifiers/PadModifierTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,18 @@ public function testModifyGrayscale(): void
5050
$this->assertColor(255, 0, 0, 255, $image->pickColor(199, 0));
5151
$this->assertColor(255, 0, 0, 255, $image->pickColor(199, 199));
5252
}
53+
54+
public function testModifyGrayscaleAlpha(): void
55+
{
56+
$image = $this->readTestImage('grayscale-alpha.png');
57+
$this->assertEquals(256, $image->width());
58+
$this->assertEquals(256, $image->height());
59+
$image->modify(new PadModifier(258, 258, 'f00'));
60+
$this->assertEquals(258, $image->width());
61+
$this->assertEquals(258, $image->height());
62+
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 0));
63+
$this->assertColor(255, 0, 0, 255, $image->pickColor(0, 257));
64+
$this->assertColor(255, 0, 0, 255, $image->pickColor(257, 0));
65+
$this->assertColor(255, 0, 0, 255, $image->pickColor(257, 257));
66+
}
5367
}
692 Bytes
Loading

0 commit comments

Comments
 (0)