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
12 changes: 9 additions & 3 deletions src/Modifiers/CropModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,22 @@ private function background(SizeInterface $resizeTo, ImageInterface $image): Vip
$bgColor->channel(Blue::class)->value(),
];

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Modifiers/CropModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,16 @@ public function testModifyCropAnimatedSmart(): void
$this->assertEquals(15, $image->height());
$this->assertColor(255, 166, 1, 255, $image->pickColor(8, 8));
}

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

// Ensure the image is encodable
$image->encode();
}
}
Binary file added tests/resources/grayscale.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.