You are here

private function AddWatermark::getMargins in Basic Watermark 8

Gets the offset of where to put the watermark dependend of its position.

Depending on the position selected we calculate the x and y offset taking into consideration the margins provided.

Parameters

array $image: The image width and height.

array $watermark: The watermark gd resource, width and height.

string $position: The position the watermark is going to be placed.

array $margins: The margins to keep around the watermark.

Return value

array The x and y offset.

1 call to AddWatermark::getMargins()
AddWatermark::execute in src/Plugin/ImageToolkit/Operation/gd/AddWatermark.php
Performs the actual manipulation on the image.

File

src/Plugin/ImageToolkit/Operation/gd/AddWatermark.php, line 177

Class

AddWatermark
Defines GD2 Add Watermark operation.

Namespace

Drupal\basic_watermark\Plugin\ImageToolkit\Operation\gd

Code

private function getMargins(array $image, array $watermark, string $position, array $margins) {
  switch ($position) {
    case 'left-top':
      return [
        'x' => $margins['left'],
        'y' => $margins['top'],
      ];
    case 'center-top':
      return [
        'x' => $image['width'] / 2 - $watermark['width'] / 2,
        'y' => $margins['top'],
      ];
    case 'right-top':
      return [
        'x' => $image['width'] - $watermark['width'] - $margins['left'],
        'y' => $margins['top'],
      ];
    case 'left-center':
      return [
        'x' => $margins['left'],
        'y' => $image['height'] / 2 - $watermark['height'] / 2,
      ];
    case 'center-center':
      return [
        'x' => $image['width'] / 2 - $watermark['width'] / 2,
        'y' => $image['height'] / 2 - $watermark['height'] / 2,
      ];
    case 'right-center':
      return [
        'x' => $image['width'] - $watermark['width'] - $margins['left'],
        'y' => $image['height'] / 2 - $watermark['height'] / 2,
      ];
    case 'left-bottom':
      return [
        'x' => $margins['left'],
        'y' => $image['height'] - $watermark['height'] - $margins['top'],
      ];
    case 'center-bottom':
      return [
        'x' => $image['width'] / 2 - $watermark['width'] / 2,
        'y' => $image['height'] - $watermark['height'] - $margins['top'],
      ];
    case 'right-bottom':
      return [
        'x' => $image['width'] - $watermark['width'] - $margins['left'],
        'y' => $image['height'] - $watermark['height'] - $margins['top'],
      ];
    default:
      return [
        'x' => $margins['left'],
        'y' => $margins['top'],
      ];
  }
}