You are here

protected function ImagePadding::execute in Image effect kit 8

Performs the actual manipulation on the image.

Image toolkit operation implementers must implement this method. This method is responsible for actually performing the operation on the image. When this method gets called, the implementer may assume all arguments, also the optional ones, to be available, validated and corrected.

Parameters

array $arguments: An associative array of arguments to be used by the toolkit operation.

Return value

bool TRUE if the operation was performed successfully, FALSE otherwise.

Overrides ImageToolkitOperationBase::execute

File

src/Plugin/ImageToolkit/Operation/gd/ImagePadding.php, line 80
Contains \Drupal\iek\Plugin\ImageToolkit\Operation\gd\ImagePadding.

Class

ImagePadding
Defines IEK - Padding operation.

Namespace

Drupal\iek\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments = []) {
  $data = $arguments;
  $width = $this
    ->getToolkit()
    ->getWidth();
  $height = $this
    ->getToolkit()
    ->getHeight();
  $padding_top = $data['padding_top'];
  $padding_right = $data['padding_right'];
  $padding_bottom = $data['padding_bottom'];
  $padding_left = $data['padding_left'];
  $bg_color = $data['bg_color'];
  $dst = imagecreatetruecolor($width, $height);
  $bg_rgb = iek_hex2rgb($bg_color);
  $bg = imagecolorallocate($dst, $bg_rgb['red'], $bg_rgb['green'], $bg_rgb['blue']);
  imagefilledrectangle($dst, 0, 0, $width, $height, $bg);
  $this
    ->getToolkit()
    ->apply('iek_image_resize', [
    'width' => $width - ($padding_left + $padding_right),
    'height' => $height - ($padding_top + $padding_bottom),
    'blank_margin' => TRUE,
    'blank_margin_bg_color' => $bg_color,
    'position' => 'middle_center',
    'x' => 0,
    'y' => 0,
  ]);
  if (!imagecopy($dst, $this
    ->getToolkit()
    ->getResource(), $padding_left, $padding_top, 0, 0, $width - ($padding_left + $padding_right), $height - ($padding_top + $padding_bottom))) {
    return FALSE;
  }
  imagedestroy($this
    ->getToolkit()
    ->getResource());

  // Update image object.
  $this
    ->getToolkit()
    ->setResource($dst);
  return TRUE;
}