From cdf1e23f052bb086871bafcc975ea6de1bb322a4 Mon Sep 17 00:00:00 2001 From: ve3 Date: Sat, 16 Mar 2024 02:07:41 +0700 Subject: [PATCH] Add option padding to watermark text and image. --- Rundiz/Image/Drivers/Gd.php | 5 +++-- Rundiz/Image/Drivers/Gd/Watermark.php | 14 ++++++++++---- Rundiz/Image/Drivers/Imagick.php | 5 +++-- Rundiz/Image/Drivers/Imagick/Watermark.php | 14 ++++++++++---- Rundiz/Image/ImageInterface.php | 5 ++++- tests/phpunit/ExtendedAbstractImage.php | 2 +- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Rundiz/Image/Drivers/Gd.php b/Rundiz/Image/Drivers/Gd.php index f46baca..a54be88 100644 --- a/Rundiz/Image/Drivers/Gd.php +++ b/Rundiz/Image/Drivers/Gd.php @@ -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; @@ -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 ); } diff --git a/Rundiz/Image/Drivers/Gd/Watermark.php b/Rundiz/Image/Drivers/Gd/Watermark.php index 8503bc8..f039ac7 100644 --- a/Rundiz/Image/Drivers/Gd/Watermark.php +++ b/Rundiz/Image/Drivers/Gd/Watermark.php @@ -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)) { @@ -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; } } @@ -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; } } diff --git a/Rundiz/Image/Drivers/Imagick.php b/Rundiz/Image/Drivers/Imagick.php index a846d92..b860fff 100644 --- a/Rundiz/Image/Drivers/Imagick.php +++ b/Rundiz/Image/Drivers/Imagick.php @@ -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; @@ -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 ); } diff --git a/Rundiz/Image/Drivers/Imagick/Watermark.php b/Rundiz/Image/Drivers/Imagick/Watermark.php index 81be6fe..edc3e3c 100644 --- a/Rundiz/Image/Drivers/Imagick/Watermark.php +++ b/Rundiz/Image/Drivers/Imagick/Watermark.php @@ -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)) { @@ -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; } } @@ -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; } } diff --git a/Rundiz/Image/ImageInterface.php b/Rundiz/Image/ImageInterface.php index 2120504..bc2e987 100644 --- a/Rundiz/Image/ImageInterface.php +++ b/Rundiz/Image/ImageInterface.php @@ -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)
+ * `padding` (int) Padding around watermark object. Use with left, right, bottom, top but not middle, center. See `\Rundiz\Image\Traits\CalculationTrait::calculateWatermarkImageStartXY()`.
* @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 = []); /** @@ -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)
* `fillBackground` (bool) Set to `true` to fill background color for text bounding box. Default is `false` to use transparent.
* `backgroundColor` (string) The background color to fill for text bounding box. Available values are 'black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'debug'.
+ * `padding` (int) (Since v3.1.3) Padding around watermark text. Use with left, right, bottom, top but not middle, center.
* @return bool Return true on success, false on failed. Call to status_msg property to see the details on failure. */ public function watermarkText( diff --git a/tests/phpunit/ExtendedAbstractImage.php b/tests/phpunit/ExtendedAbstractImage.php index d7a642c..50b965f 100644 --- a/tests/phpunit/ExtendedAbstractImage.php +++ b/tests/phpunit/ExtendedAbstractImage.php @@ -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