ImageMergeTest.php 2.2 KB

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