<?php
namespace Grafika;
class Position {
const TOP_LEFT = 'top-left';
const TOP_CENTER = 'top-center';
const TOP_RIGHT = 'top-right';
const CENTER_LEFT = 'center-left';
const CENTER = 'center';
const CENTER_RIGHT = 'center-right';
const BOTTOM_LEFT = 'bottom-left';
const BOTTOM_CENTER = 'bottom-center';
const BOTTOM_RIGHT = 'bottom-right';
private $position;
private $offsetX;
private $offsetY;
public function __construct($position='center', $offsetX=0, $offsetY=0) {
$this->position = $position;
$this->offsetX = $offsetX;
$this->offsetY = $offsetY;
}
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
);
}
public function getText() {
return $this->position;
}
public function getOffsetY() {
return $this->offsetY;
}
public function getOffsetX() {
return $this->offsetX;
}
}