Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font color #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()}\\)\"",
];
}
Expand Down
67 changes: 67 additions & 0 deletions src/Ajaxray/PHPWatermark/Watermark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 [email protected]
*/
public function setFontColor($fontColor)
{
$this->options['fontcolor'] = $this->_prepareColorForWaterMark($fontColor);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this function chainable, we should return $this.

Suggested change
$this->options['fontcolor'] = $this->_prepareColorForWaterMark($fontColor);
$this->options['fontcolor'] = $this->_prepareColorForWaterMark($fontColor);
return $this;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct I'm sorry i miss that.

}

/**
* @param float $opacity Between .1 (very transparent) to .9 (almost opaque).
*/
Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XF is not found. Can you please use an alternative that does not require additional installation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for that mistake maybe i forget to change that line because i use it in my project the line number 250 should be like this:

if (!empty($rgb[4])) {

$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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utf8_strlen function is not found. Can we use mb_strlen() instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't try that but to make utf8_strlen work you should to Install php-mbstring module on server and that's will solve this issue.

$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];
}
}