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()}\\)\"", ]; } diff --git a/src/Ajaxray/PHPWatermark/Watermark.php b/src/Ajaxray/PHPWatermark/Watermark.php index cd9acdc..f343c52 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'] = $this->_prepareColorForWaterMark($fontColor); + } + /** * @param float $opacity Between .1 (very transparent) to .9 (almost opaque). */ @@ -212,4 +225,58 @@ 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) + { + $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]; + } }