ImageMergeTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use SimpleSoftwareIO\QrCode\Image;
  4. use SimpleSoftwareIO\QrCode\ImageMerge;
  5. class ImageMergeTest extends TestCase
  6. {
  7. /**
  8. * The location to save the testing image.
  9. *
  10. * @var string
  11. */
  12. protected $testImageSaveLocation;
  13. /**
  14. * The location to save the compare image.
  15. *
  16. * @var string
  17. */
  18. protected $compareTestSaveLocation;
  19. /**
  20. * The ImageMerge Object.
  21. *
  22. * @var ImageMerge
  23. */
  24. protected $testImage;
  25. /**
  26. * The location of the test image that is having an image merged over top of it.
  27. *
  28. * @var string
  29. */
  30. protected $testImagePath;
  31. /**
  32. * The location of the test image that is being merged.
  33. * @var mixed
  34. */
  35. protected $mergeImagePath;
  36. public function setUp(): void
  37. {
  38. $this->testImagePath = file_get_contents(dirname(__FILE__).'/Images/simplesoftware-icon-grey-blue.png');
  39. $this->mergeImagePath = file_get_contents(dirname(__FILE__).'/Images/200x300.png');
  40. $this->testImage = new ImageMerge(
  41. new Image($this->testImagePath),
  42. new Image($this->mergeImagePath)
  43. );
  44. $this->testImageSaveLocation = dirname(__FILE__).'/testImage.png';
  45. $this->compareTestSaveLocation = dirname(__FILE__).'/compareImage.png';
  46. }
  47. public function tearDown(): void
  48. {
  49. @unlink($this->testImageSaveLocation);
  50. @unlink($this->compareTestSaveLocation);
  51. }
  52. public function test_it_merges_two_images_together_and_centers_it()
  53. {
  54. //We know the source image is 512x512 and the merge image is 200x300
  55. $source = imagecreatefromstring($this->testImagePath);
  56. $merge = imagecreatefromstring($this->mergeImagePath);
  57. //Create a PNG and place the image in the middle using 20% of the area.
  58. imagecopyresampled(
  59. $source,
  60. $merge,
  61. 205,
  62. 222,
  63. 0,
  64. 0,
  65. 102,
  66. 67,
  67. 536,
  68. 354
  69. );
  70. imagepng($source, $this->compareTestSaveLocation);
  71. $testImage = $this->testImage->merge(.2);
  72. file_put_contents($this->testImageSaveLocation, $testImage);
  73. $this->assertEquals(file_get_contents($this->compareTestSaveLocation), file_get_contents($this->testImageSaveLocation));
  74. }
  75. public function test_it_throws_an_exception_when_percentage_is_greater_than_1()
  76. {
  77. $this->expectException(InvalidArgumentException::class);
  78. $this->testImage->merge(1.1);
  79. }
  80. }