Skip to content

Commit

Permalink
Fix gradient pattern generation for Imagick renderer. Fixes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Jan 31, 2022
1 parent 9c1ebc4 commit 30fc87a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Renderer/Image/ImagickImageBackEnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ private function drawPath(Path $path) : void

private function createGradientFill(Gradient $gradient, float $x, float $y, float $width, float $height) : string
{
list($width, $height) = $this->matrices[$this->matrixIndex]->apply($x + $width, $y + $height);
list($x, $y) = $this->matrices[$this->matrixIndex]->apply($x, $y);
$width -= $x;
$height -= $y;
list($width, $height) = $this->matrices[$this->matrixIndex]->apply($width, $height);

$startColor = $this->getColorPixel($gradient->getStartColor())->getColorAsString();
$endColor = $this->getColorPixel($gradient->getEndColor())->getColorAsString();
Expand Down Expand Up @@ -290,8 +287,8 @@ private function createGradientFill(Gradient $gradient, float $x, float $y, floa
}

$id = sprintf('g%d', ++$this->gradientCount);
$this->draw->pushPattern($id, 0, 0, $x + $width, $y + $height);
$this->draw->composite(Imagick::COMPOSITE_COPY, $x, $y, $width, $height, $gradientImage);
$this->draw->pushPattern($id, 0, 0, $width, $height);
$this->draw->composite(Imagick::COMPOSITE_COPY, 0, 0, $width, $height, $gradientImage);
$this->draw->popPattern();
return $id;
}
Expand Down
33 changes: 33 additions & 0 deletions test/Integration/ImagickRenderingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

namespace BaconQrCodeTest\Integration;

use BaconQrCode\Renderer\Color\Rgb;
use BaconQrCode\Renderer\Eye\SquareEye;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Module\SquareModule;
use BaconQrCode\Renderer\RendererStyle\EyeFill;
use BaconQrCode\Renderer\RendererStyle\Fill;
use BaconQrCode\Renderer\RendererStyle\Gradient;
use BaconQrCode\Renderer\RendererStyle\GradientType;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use PHPUnit\Framework\TestCase;
Expand All @@ -27,4 +34,30 @@ public function testGenericQrCode() : void
$this->assertMatchesFileSnapshot($tempName);
unlink($tempName);
}

public function testIssue79() : void
{
$eye = SquareEye::instance();
$squareModule = SquareModule::instance();

$eyeFill = new EyeFill(new Rgb(100, 100, 55), new Rgb(100, 100, 255));
$gradient = new Gradient(new Rgb(100, 100, 55), new Rgb(100, 100, 255), GradientType::HORIZONTAL());

$renderer = new ImageRenderer(
new RendererStyle(
400,
2,
$squareModule,
$eye,
Fill::withForegroundGradient(new Rgb(255, 255, 255), $gradient, $eyeFill, $eyeFill, $eyeFill)
),
new ImagickImageBackEnd()
);
$writer = new Writer($renderer);
$tempName = tempnam(sys_get_temp_dir(), 'test') . '.png';
$writer->writeFile('https://apiroad.net/very-long-url', $tempName);

$this->assertMatchesFileSnapshot($tempName);
unlink($tempName);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 30fc87a

Please sign in to comment.