Skip to content

Commit 3c199ec

Browse files
author
Corey McCormick
committed
Resolve image ratio issues when merging over top of a QrCode
1 parent 1c84843 commit 3c199ec

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

src/ImageMerge.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SimpleSoftwareIO\QrCode;
44

5+
use InvalidArgumentException;
6+
57
class ImageMerge
68
{
79
/**
@@ -46,6 +48,13 @@ class ImageMerge
4648
*/
4749
protected $mergeImageWidth;
4850

51+
/**
52+
* Holds the radio of the merging image.
53+
*
54+
* @var float
55+
*/
56+
protected $mergeRatio;
57+
4958
/**
5059
* The height of the merge image after it is merged.
5160
*
@@ -131,11 +140,13 @@ protected function createImage()
131140
* Sets the objects properties.
132141
*
133142
* @param $percentage float The percentage that the merge image should take up.
143+
*
144+
* @return void
134145
*/
135146
protected function setProperties($percentage)
136147
{
137148
if ($percentage > 1) {
138-
throw new \InvalidArgumentException('$percentage must be less than 1');
149+
throw new InvalidArgumentException('$percentage must be less than 1');
139150
}
140151

141152
$this->sourceImageHeight = $this->sourceImage->getHeight();
@@ -150,21 +161,26 @@ protected function setProperties($percentage)
150161

151162
/**
152163
* Calculates the center of the source Image using the Merge image.
164+
*
165+
* @return void
153166
*/
154-
private function calculateCenter()
167+
protected function calculateCenter()
155168
{
156-
$this->centerY = ($this->sourceImageHeight / 2) - ($this->postMergeImageHeight / 2);
157-
$this->centerX = ($this->sourceImageWidth / 2) - ($this->postMergeImageHeight / 2);
169+
$this->centerX = intval(($this->sourceImageWidth / 2) - ($this->postMergeImageWidth / 2));
170+
$this->centerY = intval(($this->sourceImageHeight / 2) - ($this->postMergeImageHeight / 2));
158171
}
159172

160173
/**
161174
* Calculates the width of the merge image being placed on the source image.
162175
*
163176
* @param float $percentage
177+
*
178+
* @return void
164179
*/
165-
private function calculateOverlap($percentage)
180+
protected function calculateOverlap($percentage)
166181
{
167-
$this->postMergeImageHeight = $this->sourceImageHeight * $percentage;
168-
$this->postMergeImageWidth = $this->sourceImageWidth * $percentage;
182+
$this->mergeRatio = round($this->mergeImageWidth / $this->mergeImageHeight, 2);
183+
$this->postMergeImageWidth = intval($this->sourceImageWidth * $percentage);
184+
$this->postMergeImageHeight = intval($this->postMergeImageWidth / $this->mergeRatio);
169185
}
170186
}

tests/ImageMergeTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ class ImageMergeTest extends TestCase
2828
protected $testImage;
2929

3030
/**
31-
* The location of the test image to use.
31+
* The location of the test image that is having an image merged over top of it.
3232
*
3333
* @var string
3434
*/
3535
protected $testImagePath;
3636

37+
/**
38+
* The location of the test image that is being merged.
39+
* @var mixed
40+
*/
41+
protected $mergeImagePath;
42+
3743
public function setUp(): void
3844
{
3945
$this->testImagePath = file_get_contents(dirname(__FILE__).'/Images/simplesoftware-icon-grey-blue.png');
46+
$this->mergeImagePath = file_get_contents(dirname(__FILE__).'/Images/200x300.png');
4047
$this->testImage = new ImageMerge(
4148
new Image($this->testImagePath),
42-
new Image($this->testImagePath)
49+
new Image($this->mergeImagePath)
4350
);
4451

4552
$this->testImageSaveLocation = dirname(__FILE__).'/testImage.png';
@@ -54,22 +61,22 @@ public function tearDown(): void
5461

5562
public function test_it_merges_two_images_together_and_centers_it()
5663
{
57-
//We know the test image is 512x512
64+
//We know the source image is 512x512 and the merge image is 200x300
5865
$source = imagecreatefromstring($this->testImagePath);
59-
$merge = imagecreatefromstring($this->testImagePath);
66+
$merge = imagecreatefromstring($this->mergeImagePath);
6067

6168
//Create a PNG and place the image in the middle using 20% of the area.
6269
imagecopyresampled(
6370
$source,
6471
$merge,
65-
204,
66-
204,
72+
205,
73+
222,
6774
0,
6875
0,
6976
102,
70-
102,
71-
512,
72-
512
77+
67,
78+
536,
79+
354
7380
);
7481
imagepng($source, $this->compareTestSaveLocation);
7582

tests/Images/200x300.png

232 KB
Loading

0 commit comments

Comments
 (0)