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