Grafika\Position::getXY PHP Method

getXY() public method

Translate the textual position + offsets into x,y values.
public getXY ( integer $canvasWidth, integer $canvasHeight, integer $imageWidth, integer $imageHeight ) : array
$canvasWidth integer Width of canvas.
$canvasHeight integer Height of canvas.
$imageWidth integer Width of image/object added.
$imageHeight integer Height of image/object added.
return array Array of X and Y coordinates: array($x, $y).
    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);
    }

Usage Example

Beispiel #1
0
 /**
  * Crop the image to the given dimension and position.
  *
  * @param Image $image
  * @param int $cropWidth Crop width in pixels.
  * @param int $cropHeight Crop Height in pixels.
  * @param string $position The crop position. Possible values top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart. Defaults to center.
  * @param int $offsetX Number of pixels to add to the X position of the crop.
  * @param int $offsetY Number of pixels to add to the Y position of the crop.
  *
  * @return Editor
  * @throws \Exception
  */
 public function crop(&$image, $cropWidth, $cropHeight, $position = 'center', $offsetX = 0, $offsetY = 0)
 {
     if ($image->isAnimated()) {
         // Ignore animated GIF for now
         return $this;
     }
     if ('smart' === $position) {
         // Smart crop
         list($x, $y) = $this->_smartCrop($image, $cropWidth, $cropHeight);
     } else {
         // Turn into an instance of Position
         $position = new Position($position, $offsetX, $offsetY);
         // Crop position as x,y coordinates
         list($x, $y) = $position->getXY($image->getWidth(), $image->getHeight(), $cropWidth, $cropHeight);
     }
     $x += $offsetX;
     $y += $offsetY;
     $image->getCore()->cropImage($cropWidth, $cropHeight, $x, $y);
     return $this;
 }
All Usage Examples Of Grafika\Position::getXY