Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/ImageMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SimpleSoftwareIO\QrCode;

use InvalidArgumentException;

class ImageMerge
{
/**
Expand Down Expand Up @@ -46,6 +48,13 @@ class ImageMerge
*/
protected $mergeImageWidth;

/**
* Holds the radio of the merging image.
*
* @var float
*/
protected $mergeRatio;

/**
* The height of the merge image after it is merged.
*
Expand Down Expand Up @@ -131,11 +140,13 @@ protected function createImage()
* Sets the objects properties.
*
* @param $percentage float The percentage that the merge image should take up.
*
* @return void
*/
protected function setProperties($percentage)
{
if ($percentage > 1) {
throw new \InvalidArgumentException('$percentage must be less than 1');
throw new InvalidArgumentException('$percentage must be less than 1');
}

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

/**
* Calculates the center of the source Image using the Merge image.
*
* @return void
*/
private function calculateCenter()
protected function calculateCenter()
{
$this->centerY = ($this->sourceImageHeight / 2) - ($this->postMergeImageHeight / 2);
$this->centerX = ($this->sourceImageWidth / 2) - ($this->postMergeImageHeight / 2);
$this->centerX = intval(($this->sourceImageWidth / 2) - ($this->postMergeImageWidth / 2));
$this->centerY = intval(($this->sourceImageHeight / 2) - ($this->postMergeImageHeight / 2));
}

/**
* Calculates the width of the merge image being placed on the source image.
*
* @param float $percentage
*
* @return void
*/
private function calculateOverlap($percentage)
protected function calculateOverlap($percentage)
{
$this->postMergeImageHeight = $this->sourceImageHeight * $percentage;
$this->postMergeImageWidth = $this->sourceImageWidth * $percentage;
$this->mergeRatio = round($this->mergeImageWidth / $this->mergeImageHeight, 2);
$this->postMergeImageWidth = intval($this->sourceImageWidth * $percentage);
$this->postMergeImageHeight = intval($this->postMergeImageWidth / $this->mergeRatio);
}
}
25 changes: 16 additions & 9 deletions tests/ImageMergeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ class ImageMergeTest extends TestCase
protected $testImage;

/**
* The location of the test image to use.
* The location of the test image that is having an image merged over top of it.
*
* @var string
*/
protected $testImagePath;

/**
* The location of the test image that is being merged.
* @var mixed
*/
protected $mergeImagePath;

public function setUp(): void
{
$this->testImagePath = file_get_contents(dirname(__FILE__).'/Images/simplesoftware-icon-grey-blue.png');
$this->mergeImagePath = file_get_contents(dirname(__FILE__).'/Images/200x300.png');
$this->testImage = new ImageMerge(
new Image($this->testImagePath),
new Image($this->testImagePath)
new Image($this->mergeImagePath)
);

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

public function test_it_merges_two_images_together_and_centers_it()
{
//We know the test image is 512x512
//We know the source image is 512x512 and the merge image is 200x300
$source = imagecreatefromstring($this->testImagePath);
$merge = imagecreatefromstring($this->testImagePath);
$merge = imagecreatefromstring($this->mergeImagePath);

//Create a PNG and place the image in the middle using 20% of the area.
imagecopyresampled(
$source,
$merge,
204,
204,
205,
222,
0,
0,
102,
102,
512,
512
67,
536,
354
);
imagepng($source, $this->compareTestSaveLocation);

Expand Down
Binary file added tests/Images/200x300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.