Position.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Grafika;
  3. /**
  4. * Hold and computes position of objects added to canvas.
  5. *
  6. * @package Grafika
  7. */
  8. class Position {
  9. /**
  10. * Top left of the canvas.
  11. */
  12. const TOP_LEFT = 'top-left';
  13. /**
  14. * Top center of the canvas.
  15. */
  16. const TOP_CENTER = 'top-center';
  17. /**
  18. * Top right of the canvas.
  19. */
  20. const TOP_RIGHT = 'top-right';
  21. /**
  22. * Center left of the canvas.
  23. */
  24. const CENTER_LEFT = 'center-left';
  25. /**
  26. * Center of the canvas.
  27. */
  28. const CENTER = 'center';
  29. /**
  30. * Center right of the canvas.
  31. */
  32. const CENTER_RIGHT = 'center-right';
  33. /**
  34. * Center left of the canvas.
  35. */
  36. const BOTTOM_LEFT = 'bottom-left';
  37. /**
  38. * Bottom center of the canvas.
  39. */
  40. const BOTTOM_CENTER = 'bottom-center';
  41. /**
  42. * Bottom right of the canvas.
  43. */
  44. const BOTTOM_RIGHT = 'bottom-right';
  45. /**
  46. * @var string Holds position in human-readable text.
  47. */
  48. private $position;
  49. /**
  50. * @var int Number of pixels to the left of the origin
  51. */
  52. private $offsetX;
  53. /**
  54. * @var int Number of pixels to the bottom of the origin.
  55. */
  56. private $offsetY;
  57. /**
  58. * Position constructor.
  59. *
  60. * @param string $position Defaults to center.
  61. * @param int $offsetX Defaults to 0.
  62. * @param int $offsetY Defaults to 0.
  63. */
  64. public function __construct($position='center', $offsetX=0, $offsetY=0) {
  65. $this->position = $position;
  66. $this->offsetX = $offsetX;
  67. $this->offsetY = $offsetY;
  68. }
  69. /**
  70. * Translate the textual position + offsets into x,y values.
  71. *
  72. * @param int $canvasWidth Width of canvas.
  73. * @param int $canvasHeight Height of canvas.
  74. * @param int $imageWidth Width of image/object added.
  75. * @param int $imageHeight Height of image/object added.
  76. *
  77. * @return array Array of X and Y coordinates: array($x, $y).
  78. * @throws \Exception When invalid position.
  79. */
  80. public function getXY($canvasWidth, $canvasHeight, $imageWidth, $imageHeight){
  81. if ( self::TOP_LEFT === $this->position) {
  82. $x = 0;
  83. $y = 0;
  84. } else if ( self::TOP_CENTER === $this->position) {
  85. $x = (int)round(($canvasWidth / 2) - ($imageWidth / 2));
  86. $y = 0;
  87. } else if ( self::TOP_RIGHT === $this->position) {
  88. $x = $canvasWidth - $imageWidth;
  89. $y = 0;
  90. } else if ( self::CENTER_LEFT === $this->position) {
  91. $x = 0;
  92. $y = (int)round(($canvasHeight / 2) - ($imageHeight / 2));
  93. } else if ( self::CENTER_RIGHT === $this->position) {
  94. $x = $canvasWidth - $imageWidth;
  95. $y = (int)round(($canvasHeight / 2) - ($imageHeight / 2));
  96. } else if ( self::BOTTOM_LEFT === $this->position) {
  97. $x = 0;
  98. $y = $canvasHeight - $imageHeight;
  99. } else if ( self::BOTTOM_CENTER === $this->position) {
  100. $x = (int)round(($canvasWidth / 2) - ($imageWidth / 2));
  101. $y = $canvasHeight - $imageHeight;
  102. } else if ( self::BOTTOM_RIGHT === $this->position) {
  103. $x = $canvasWidth - $imageWidth;
  104. $y = $canvasHeight - $imageHeight;
  105. } else if ( self::CENTER === $this->position) {
  106. $x = (int)round(($canvasWidth / 2) - ($imageWidth / 2));
  107. $y = (int)round(($canvasHeight / 2) - ($imageHeight / 2));
  108. } else {
  109. throw new \Exception( sprintf( 'Invalid position "%s".', $this->position ) );
  110. }
  111. return array(
  112. $x + $this->offsetX,
  113. $y + $this->offsetY
  114. );
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getText() {
  120. return $this->position;
  121. }
  122. /**
  123. * @return int
  124. */
  125. public function getOffsetY() {
  126. return $this->offsetY;
  127. }
  128. /**
  129. * @return int
  130. */
  131. public function getOffsetX() {
  132. return $this->offsetX;
  133. }
  134. }