You are here

public static function Watermark::addWatermark in Media watermark 8

Function to add watermark.

Parameters

object $main_img_obj: Image object to add watermark.

object $watermark_img_obj: Watermark image object.

\Drupal\media_watermark\Entity\MediaWatermark $watermark: Watermark position object.

$ext: Uploaded image extension.

Return value

mixed return resource of image with watermark already add

1 call to Watermark::addWatermark()
Watermark::createImage in src/Watermark/Watermark.php
Function to create image (.jpg, .jpeg, .png, .gif) file.

File

src/Watermark/Watermark.php, line 30

Class

Watermark
Custom watermark class.

Namespace

Drupal\media_watermark\Watermark

Code

public static function addWatermark($main_img_obj, $watermark_img_obj, $watermark, $ext) {
  $main_img_obj_w = imagesx($main_img_obj);
  $main_img_obj_h = imagesy($main_img_obj);
  $watermark_img_obj_w = imagesx($watermark_img_obj);
  $watermark_img_obj_h = imagesy($watermark_img_obj);
  $vm = $watermark
    ->getVerticalMargin();
  $hm = $watermark
    ->getHorizontalMargin();
  switch ($watermark
    ->getHorizontalPosition()) {
    case 'left':
      $margin_x = $hm;
      break;
    case 'middle':

      // Determine center position coordinates.
      $margin_x = floor($main_img_obj_w / 2 - $watermark_img_obj_w / 2) + $hm;
      break;
    case 'right':
      $margin_x = $main_img_obj_w - $watermark_img_obj_w + $hm;
      break;
  }
  switch ($watermark
    ->getVerticalPosition()) {
    case 'top':
      $margin_y = $vm;
      break;
    case 'center':
      $margin_y = floor($main_img_obj_h / 2 - $watermark_img_obj_h / 2) + $hm;
      break;
    case 'bottom':
      $margin_y = $main_img_obj_h - $watermark_img_obj_h + $vm;
      break;
  }

  // Set the margins for stamp and get the height/width of the stamp image.
  $sx = imagesx($watermark_img_obj);
  $sy = imagesy($watermark_img_obj);
  imagecopy($main_img_obj, $watermark_img_obj, $margin_x, $margin_y, 0, 0, $sx, $sy);
  imagejpeg($main_img_obj);
  return $main_img_obj;
}