From 5684c6ed720ab459e463bdd54fd81310554005fd Mon Sep 17 00:00:00 2001 From: shqawe1 Date: Mon, 14 Aug 2023 22:06:50 +0300 Subject: [PATCH 1/3] Update Watermark.php Adding two functions on to change font color and another for convert rgba color to accept format for ImageMagick command . --- src/Ajaxray/PHPWatermark/Watermark.php | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Ajaxray/PHPWatermark/Watermark.php b/src/Ajaxray/PHPWatermark/Watermark.php index cd9acdc..6462447 100644 --- a/src/Ajaxray/PHPWatermark/Watermark.php +++ b/src/Ajaxray/PHPWatermark/Watermark.php @@ -44,6 +44,7 @@ class Watermark 'opacity' => 0.3, 'rotate' => 0, 'style' => 1, // STYLE_IMG_DISSOLVE or STYLE_TEXT_BEVEL + 'fontcolor' => '#ffffff', ]; public function __construct(string $source) @@ -160,6 +161,18 @@ public function setFontSize(int $fontSize): self return $this; } + /** + * Font color. Should to be in rgb or rgba format + * + * @param string $fontColor + * @return Watermark + * Added by shqawe@gmail.com + */ + public function setFontColor($fontColor) + { + $this->options['fontcolor'] = $fontColor; + } + /** * @param float $opacity Between .1 (very transparent) to .9 (almost opaque). */ @@ -212,4 +225,45 @@ private function ensureWritable(string $dirPath): void throw new \InvalidArgumentException("The specified destination $dirPath is not writable!"); } } + + private function _colorToRGB($hex) + { + $hex = strtolower($hex); + + if (strpos($hex, 'rgba') !== false) { + preg_match('/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/i', $hex, $rgb); + + if ($rgb) { + if (!empty($rgb[4]) && \XF::options()->up_wm_opecity === 0) { + $this->setOpacity(intval(127 - 127 * $rgb[4])); + } + + return [$rgb[1], $rgb[2], $rgb[3]]; + } + } + + if (strpos($hex, 'rgb') !== false) { + preg_match('/^rgb\(\s*(\d+%?)\s*,\s*(\d+%?)\s*,\s*(\d+%?)\s*\)$/i', $hex, $rgb); + + if ($rgb) { + return [$rgb[1], $rgb[2], $rgb[3]]; + } + } else { + $hex = str_replace('#', '', $hex); + + if (utf8_strlen($hex) == 3) { + $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1)); + $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1)); + $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1)); + } else { + $r = hexdec(substr($hex, 0, 2)); + $g = hexdec(substr($hex, 2, 2)); + $b = hexdec(substr($hex, 4, 2)); + } + + return [$r, $g, $b]; + } + + return [0, 0, 0]; + } } From 9b27fca8f218d7311909fa3b468a74aa6c75bde1 Mon Sep 17 00:00:00 2001 From: shqawe1 Date: Mon, 14 Aug 2023 22:08:40 +0300 Subject: [PATCH 2/3] Update ImageCommandBuilder.php Adding format font color function to getDuelTextColor function. --- .../PHPWatermark/CommandBuilders/ImageCommandBuilder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Ajaxray/PHPWatermark/CommandBuilders/ImageCommandBuilder.php b/src/Ajaxray/PHPWatermark/CommandBuilders/ImageCommandBuilder.php index 57cae0e..60779be 100644 --- a/src/Ajaxray/PHPWatermark/CommandBuilders/ImageCommandBuilder.php +++ b/src/Ajaxray/PHPWatermark/CommandBuilders/ImageCommandBuilder.php @@ -50,7 +50,8 @@ public function getTextMarkCommand(string $text, string $output, array $options) protected function getDuelTextColor(): array { return [ - "fill \"rgba\\(255,255,255,{$this->getOpacity()}\\)\"", + // "fill \"rgba\\(255,255,255,{$this->getOpacity()}\\)\"", + 'fill "' . $this->options['fontcolor'] . '\ "', "fill \"rgba\\(0,0,0,{$this->getOpacity()}\\)\"", ]; } From 9707f404561bd49c49d975543ec23b84372cca40 Mon Sep 17 00:00:00 2001 From: shqawe1 Date: Mon, 14 Aug 2023 22:28:55 +0300 Subject: [PATCH 3/3] Update Watermark.php Add miss function to prepare font color --- src/Ajaxray/PHPWatermark/Watermark.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Ajaxray/PHPWatermark/Watermark.php b/src/Ajaxray/PHPWatermark/Watermark.php index 6462447..f343c52 100644 --- a/src/Ajaxray/PHPWatermark/Watermark.php +++ b/src/Ajaxray/PHPWatermark/Watermark.php @@ -170,7 +170,7 @@ public function setFontSize(int $fontSize): self */ public function setFontColor($fontColor) { - $this->options['fontcolor'] = $fontColor; + $this->options['fontcolor'] = $this->_prepareColorForWaterMark($fontColor); } /** @@ -225,6 +225,19 @@ private function ensureWritable(string $dirPath): void throw new \InvalidArgumentException("The specified destination $dirPath is not writable!"); } } + + /** + * Prepare color for watermark command line + */ + private function _prepareColorForWaterMark($color) + { + $hexColor = $this->_colorToRGB($color); + + $opecity = $this->options['opacity']; + + $rgba = "rgba\\(" . $hexColor[0] . "," . $hexColor[1] . "," . $hexColor[2] . "," . $opecity . "\\)"; + return $rgba; + } private function _colorToRGB($hex) {