Skip to content

Commit c133e18

Browse files
committed
Image: fixed lost alpha after imagecrop() [Closes #202]
1 parent 5621771 commit c133e18

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

src/Utils/Image.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ public function crop($left, $top, $width, $height)
366366
[$r['x'], $r['y'], $r['width'], $r['height']]
367367
= static::calculateCutout($this->getWidth(), $this->getHeight(), $left, $top, $width, $height);
368368
$this->image = imagecrop($this->image, $r);
369+
imagesavealpha($this->image, true);
369370
return $this;
370371
}
371372

tests/Utils/Image.resize.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ test(function () use ($main) { // rotate
140140
});
141141

142142

143+
test(function () use ($main) { // alpha crop
144+
$image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
145+
$image->crop(1, 1, 8, 8);
146+
Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.crop.gd2'), toGd2($image));
147+
if (PHP_VERSION_ID < 70200) {
148+
Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.crop-71.png'), $image->toString($image::PNG));
149+
} else {
150+
Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.crop.png'), $image->toString($image::PNG));
151+
}
152+
});
153+
154+
143155
test(function () use ($main) { // alpha resize
144156
$image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
145157
$image->resize(20, 20);
203 Bytes
Loading
279 Bytes
Binary file not shown.
224 Bytes
Loading

0 commit comments

Comments
 (0)