JoinDraw.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Enable to join 2 BCGDrawing or 2 image object to make only one image.
  6. * There are some options for alignment.
  7. *
  8. *--------------------------------------------------------------------
  9. * Copyright (C) Jean-Sebastien Goupil
  10. * http://www.barcodephp.com
  11. */
  12. class JoinDraw {
  13. const ALIGN_RIGHT = 0;
  14. const ALIGN_BOTTOM = 0;
  15. const ALIGN_LEFT = 1;
  16. const ALIGN_TOP = 1;
  17. const ALIGN_CENTER = 2;
  18. const POSITION_RIGHT = 0;
  19. const POSITION_BOTTOM = 1;
  20. const POSITION_LEFT = 2;
  21. const POSITION_TOP = 3;
  22. private $image1;
  23. private $image2;
  24. private $alignement;
  25. private $position;
  26. private $space;
  27. private $im;
  28. /**
  29. * Construct the JoinDrawing Object.
  30. * - $image1 and $image2 have to be BCGDrawing object or image object.
  31. * - $space is the space between the two graphics in pixel.
  32. * - $position is the position of the $image2 depending the $image1.
  33. * - $alignment is the alignment of the $image2 if this one is smaller than $image1;
  34. * if $image2 is bigger than $image1, the $image1 will be positionned on the opposite side specified.
  35. *
  36. * @param mixed $image1
  37. * @param mixed $image2
  38. * @param BCGColor $background
  39. * @param int $space
  40. * @param int $position
  41. * @param int $alignment
  42. */
  43. public function __construct($image1, $image2, $background, $space = 10, $position = self::POSITION_RIGHT, $alignment = self::ALIGN_TOP) {
  44. if ($image1 instanceof BCGDrawing) {
  45. $this->image1 = $image1->get_im();
  46. } else {
  47. $this->image1 = $image1;
  48. }
  49. if ($image2 instanceof BCGDrawing) {
  50. $this->image2 = $image2->get_im();
  51. } else {
  52. $this->image2 = $image2;
  53. }
  54. $this->background = $background;
  55. $this->space = (int)$space;
  56. $this->position = (int)$position;
  57. $this->alignment = (int)$alignment;
  58. $this->createIm();
  59. }
  60. /**
  61. * Destroys the image.
  62. */
  63. public function __destruct() {
  64. imagedestroy($this->im);
  65. }
  66. /**
  67. * Finds the position where the barcode should be aligned.
  68. *
  69. * @param int $size1
  70. * @param int $size2
  71. * @param int $ailgnment
  72. * @return int
  73. */
  74. private function findPosition($size1, $size2, $alignment) {
  75. $rsize1 = max($size1, $size2);
  76. $rsize2 = min($size1, $size2);
  77. if ($alignment === self::ALIGN_LEFT) { // Or TOP
  78. return 0;
  79. } elseif ($alignment === self::ALIGN_CENTER) {
  80. return $rsize1 / 2 - $rsize2 / 2;
  81. } else { // RIGHT or TOP
  82. return $rsize1 - $rsize2;
  83. }
  84. }
  85. /**
  86. * Change the alignments.
  87. *
  88. * @param int $alignment
  89. * @return int
  90. */
  91. private function changeAlignment($alignment) {
  92. if ($alignment === 0) {
  93. return 1;
  94. } elseif ($alignment === 1) {
  95. return 0;
  96. } else {
  97. return 2;
  98. }
  99. }
  100. /**
  101. * Creates the image.
  102. */
  103. private function createIm() {
  104. $w1 = imagesx($this->image1);
  105. $w2 = imagesx($this->image2);
  106. $h1 = imagesy($this->image1);
  107. $h2 = imagesy($this->image2);
  108. if ($this->position === self::POSITION_LEFT || $this->position === self::POSITION_RIGHT) {
  109. $w = $w1 + $w2 + $this->space;
  110. $h = max($h1, $h2);
  111. } else {
  112. $w = max($w1, $w2);
  113. $h = $h1 + $h2 + $this->space;
  114. }
  115. $this->im = imagecreatetruecolor($w, $h);
  116. imagefill($this->im, 0, 0, $this->background->allocate($this->im));
  117. // We start defining position of images
  118. if ($this->position === self::POSITION_TOP) {
  119. if ($w1 > $w2) {
  120. $posX1 = 0;
  121. $posX2 = $this->findPosition($w1, $w2, $this->alignment);
  122. } else {
  123. $a = $this->changeAlignment($this->alignment);
  124. $posX1 = $this->findPosition($w1, $w2, $a);
  125. $posX2 = 0;
  126. }
  127. $posY2 = 0;
  128. $posY1 = $h2 + $this->space;
  129. } elseif ($this->position === self::POSITION_LEFT) {
  130. if ($w1 > $w2) {
  131. $posY1 = 0;
  132. $posY2 = $this->findPosition($h1, $h2, $this->alignment);
  133. } else {
  134. $a = $this->changeAlignment($this->alignment);
  135. $posY2 = 0;
  136. $posY1 = $this->findPosition($h1, $h2, $a);
  137. }
  138. $posX2 = 0;
  139. $posX1 = $w2 + $this->space;
  140. } elseif ($this->position === self::POSITION_BOTTOM) {
  141. if ($w1 > $w2) {
  142. $posX2 = $this->findPosition($w1, $w2, $this->alignment);
  143. $posX1 = 0;
  144. } else {
  145. $a = $this->changeAlignment($this->alignment);
  146. $posX2 = 0;
  147. $posX1 = $this->findPosition($w1, $w2, $a);
  148. }
  149. $posY1 = 0;
  150. $posY2 = $h1 + $this->space;
  151. } else { // defaults to RIGHT
  152. if ($w1 > $w2) {
  153. $posY2 = $this->findPosition($h1, $h2, $this->alignment);
  154. $posY1 = 0;
  155. } else {
  156. $a = $this->changeAlignment($this->alignment);
  157. $posY2 = 0;
  158. $posY1 = $this->findPosition($h1, $h2, $a);
  159. }
  160. $posX1 = 0;
  161. $posX2 = $w1 + $this->space;
  162. }
  163. imagecopy($this->im, $this->image1, $posX1, $posY1, 0, 0, $w1, $h1);
  164. imagecopy($this->im, $this->image2, $posX2, $posY2, 0, 0, $w2, $h2);
  165. }
  166. /**
  167. * Returns the new $im created.
  168. *
  169. * @return resource
  170. */
  171. public function get_im() {
  172. return $this->im;
  173. }
  174. }
  175. ?>