Skip to content

Commit

Permalink
Merge pull request #708 from ninze/develop
Browse files Browse the repository at this point in the history
applyMask() fix in Gd/Image
  • Loading branch information
mlocati committed Jun 13, 2021
2 parents b6b6315 + 050dd35 commit 5da0c38
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ public function applyMask(ImageInterface $mask)
$position = new Point($x, $y);
$color = $this->getColorAt($position);
$maskColor = $mask->getColorAt($position);
$round = (int) round(max($color->getAlpha(), (100 - $color->getAlpha()) * $maskColor->getRed() / 255));
$delta = (int) round($color->getAlpha() * $maskColor->getRed() / 255) * -1;

if (false === imagesetpixel($this->resource, $x, $y, $this->getColor($color->dissolve($round - $color->getAlpha())))) {
if (false === imagesetpixel($this->resource, $x, $y, $this->getColor($color->dissolve($delta)))) {
throw new RuntimeException('Apply mask operation failed');
}
}
Expand Down
59 changes: 40 additions & 19 deletions tests/tests/Image/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@

namespace Imagine\Test\Image;

use Imagine\Image\Box;
use Imagine\Image\Fill\Gradient\Horizontal;
use Imagine\Image\ImageInterface;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
use Imagine\Test\ImagineTestCase;
use Imagine\Image\Profile;
use Imagine\Image\Point\Center;
use Imagine\Image\PointSigned;
use Imagine\Image\Profile;
use Imagine\Test\ImagineTestCase;
use Imagine\Image\Point;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ImageInterface;
use Imagine\Image\Fill\Gradient\Horizontal;
use Imagine\Image\Box;

abstract class AbstractImageTest extends ImagineTestCase
{
Expand Down Expand Up @@ -513,18 +514,38 @@ public function testCreateTransparentGradient()
public function testMask()
{
$factory = $this->getImagine();

$image = $factory->open(IMAGINE_TEST_FIXTURESFOLDER . '/google.png');

$filename = $this->getTemporaryFilename('.png');
$image->applyMask($image->mask())
->save($filename);

$size = $factory->open($filename)
->getSize();

$this->assertEquals(364, $size->getWidth());
$this->assertEquals(126, $size->getHeight());
$palette = new RGB();
$box = new Box(3, 1);

// Create 3x1 px red image
$image = $factory->create($box, $palette->color('f00')); // red bg

// Create 3x1 mask with px values: [000, 808080, fff]
$mask = $factory->create($box, $palette->color('000')); // black bg
$mask->draw()
->dot(new Point(1, 0), $palette->color('808080'))
->dot(new Point(2, 0), $palette->color('fff'));

$image->applyMask($mask);

// Test all pixel values
$px = $image->getColorAt(new Point(0, 0));
$this->assertEquals(100, $px->getAlpha());
$this->assertEquals(255, $px->getValue(ColorInterface::COLOR_RED));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_GREEN));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_BLUE));

$px = $image->getColorAt(new Point(1, 0));
$this->assertEquals(50, $px->getAlpha());
$this->assertEquals(255, $px->getValue(ColorInterface::COLOR_RED));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_GREEN));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_BLUE));

$px = $image->getColorAt(new Point(2, 0));
$this->assertEquals(0, $px->getAlpha());
$this->assertEquals(255, $px->getValue(ColorInterface::COLOR_RED));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_GREEN));
$this->assertEquals(0, $px->getValue(ColorInterface::COLOR_BLUE));
}

public function testColorHistogram()
Expand Down

0 comments on commit 5da0c38

Please sign in to comment.