Skip to content

Commit 3367202

Browse files
committed
feat: convert grayscale to rgb before cropping
1 parent 628cc1d commit 3367202

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/Modifiers/CropModifier.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +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) {
81+
// Grayscale -> RGB
82+
$originalImage = $originalImage->colourspace('srgb');
83+
}
84+
7985
// original image and background must have the same number of bands
80-
if ($image->core()->native()->hasAlpha()) {
86+
if ($originalImage->hasAlpha()) {
8187
$bands[] = $bgColor->channel(Alpha::class)->value();
8288
}
8389

8490
return VipsImage::black(1, 1)
8591
->add($bgColor->channel(Red::class)->value())
86-
->cast($image->core()->native()->format)
92+
->cast($originalImage->format)
8793
->embed(0, 0, $resizeTo->width(), $resizeTo->height(), ['extend' => Extend::COPY])
88-
->copy(['interpretation' => $image->core()->native()->interpretation])
94+
->copy(['interpretation' => $originalImage->interpretation])
8995
->bandjoin($bands);
9096
}
9197

0 commit comments

Comments
 (0)