Skip to content

Commit

Permalink
Add option padding to watermark text and image.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Mar 15, 2024
1 parent a6a4a08 commit cdf1e23
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Rundiz/Image/Drivers/Gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public function show($file_ext = '')
/**
* {@inheritDoc}
*/
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0)
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0, array $options = [])
{
if (false === $this->isClassSetup()) {
return false;
Expand Down Expand Up @@ -410,7 +410,8 @@ public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_
imagesx($this->source_image_object),
imagesy($this->source_image_object),
$this->watermark_image_width,
$this->watermark_image_height
$this->watermark_image_height,
$options
);
}

Expand Down
14 changes: 10 additions & 4 deletions Rundiz/Image/Drivers/Gd/Watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public function applyText(
$wm_txt_start_y = intval($wm_txt_start_y);
}

if (!array_key_exists('padding', $options) || !is_numeric($options['padding'])) {
$options['padding'] = 10;
} elseif (isset($options['padding']) && is_numeric($options['padding'])) {
$options['padding'] = intval($options['padding']);
}

// if start x or y is NOT number, find the real position of start x or y from word left, center, right, top, middle, bottom
if (!is_numeric($wm_txt_start_x) || !is_numeric($wm_txt_start_y)) {
if (!is_numeric($wm_txt_start_x)) {
Expand All @@ -116,13 +122,13 @@ public function applyText(
break;
case 'right':
$image_width = imagesx($this->Gd->source_image_object);
$wm_txt_start_x = intval(($image_width - $wm_txt_width) - 10);// add blank space to right.
$wm_txt_start_x = intval(($image_width - $wm_txt_width) - $options['padding']);// add blank space to right.

unset($image_width);
break;
case 'left':
default:
$wm_txt_start_x = 10;// add blank space to left.
$wm_txt_start_x = $options['padding'];// add blank space to left.
break;
}
}
Expand All @@ -139,12 +145,12 @@ public function applyText(
break;
case 'bottom':
$image_height = imagesy($this->Gd->source_image_object);
$wm_txt_start_y = intval($image_height - ($wm_txt_height + 10));// add blank space to bottom.
$wm_txt_start_y = intval($image_height - ($wm_txt_height + $options['padding']));// add blank space to bottom.
unset($image_height);
break;
case 'top':
default:
$wm_txt_start_y = 10;// add blank space to top.
$wm_txt_start_y = $options['padding'];// add blank space to top.
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions Rundiz/Image/Drivers/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ private function verifyImagickVersion()
/**
* {@inheritDoc}
*/
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0)
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0, array $options = [])
{
if (false === $this->isClassSetup()) {
return false;
Expand Down Expand Up @@ -482,7 +482,8 @@ public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_
$this->Imagick->getImageWidth(),
$this->Imagick->getImageHeight(),
$this->watermark_image_width,
$this->watermark_image_height
$this->watermark_image_height,
$options
);
}

Expand Down
14 changes: 10 additions & 4 deletions Rundiz/Image/Drivers/Imagick/Watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public function applyText(
$wm_txt_start_y = intval($wm_txt_start_y);
}

if (!array_key_exists('padding', $options) || !is_numeric($options['padding'])) {
$options['padding'] = 10;
} elseif (isset($options['padding']) && is_numeric($options['padding'])) {
$options['padding'] = intval($options['padding']);
}

// if start x or y is NOT number, find the real position of start x or y from word left, center, right, top, middle, bottom
if (!is_numeric($wm_txt_start_x) || !is_numeric($wm_txt_start_y)) {
if (!is_numeric($wm_txt_start_x)) {
Expand All @@ -132,13 +138,13 @@ public function applyText(
break;
case 'right':
$image_width = $this->ImagickD->Imagick->getImageWidth();
$wm_txt_start_x = intval(($image_width - $wm_txt_width) - 10);// add blank space to right.
$wm_txt_start_x = intval(($image_width - $wm_txt_width) - $options['padding']);// add blank space to right.

unset($image_width);
break;
case 'left':
default:
$wm_txt_start_x = 10;// add blank space to left.
$wm_txt_start_x = $options['padding'];// add blank space to left.
break;
}
}
Expand All @@ -155,12 +161,12 @@ public function applyText(
break;
case 'bottom':
$image_height = $this->ImagickD->Imagick->getImageHeight();
$wm_txt_start_y = intval($image_height - (($wm_txt_height + 10) - $baseline));// add blank space to bottom.
$wm_txt_start_y = intval($image_height - (($wm_txt_height + $options['padding']) - $baseline));// add blank space to bottom.
unset($image_height);
break;
case 'top':
default:
$wm_txt_start_y = 10;// add blank space to top.
$wm_txt_start_y = $options['padding'];// add blank space to top.
break;
}
}
Expand Down
5 changes: 4 additions & 1 deletion Rundiz/Image/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ public function show($file_ext = '');
* @param string $wm_img_path Full path of watermark image file
* @param int|string $wm_img_start_x Position to begin in x axis. The value is integer or 'left', 'center', 'right'.
* @param int|string $wm_img_start_y Position to begin in y axis. The value is integer or 'top', 'middle', 'bottom'.
* @param array $options The watermark options. (Since v3.1.3)<br>
* `padding` (int) Padding around watermark object. Use with left, right, bottom, top but not middle, center. See `\Rundiz\Image\Traits\CalculationTrait::calculateWatermarkImageStartXY()`.<br>
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
*/
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0);
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0, array $options = []);


/**
Expand All @@ -120,6 +122,7 @@ public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_
* @param array $options The watermark text options. (Since v.3.1.0)<br>
* `fillBackground` (bool) Set to `true` to fill background color for text bounding box. Default is `false` to use transparent.<br>
* `backgroundColor` (string) The background color to fill for text bounding box. Available values are 'black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'debug'.<br>
* `padding` (int) (Since v3.1.3) Padding around watermark text. Use with left, right, bottom, top but not middle, center.<br>
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure.
*/
public function watermarkText(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/ExtendedAbstractImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function verifyMasterDimension()
}// verifyMasterDimension


public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0)
public function watermarkImage($wm_img_path, $wm_img_start_x = 0, $wm_img_start_y = 0, array $options = [])
{
}// watermarkImage

Expand Down

0 comments on commit cdf1e23

Please sign in to comment.