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

Text color #25

Open
shqawe1 opened this issue Aug 12, 2023 · 1 comment
Open

Text color #25

shqawe1 opened this issue Aug 12, 2023 · 1 comment
Labels

Comments

@shqawe1
Copy link

shqawe1 commented Aug 12, 2023

Hi,

Is there any plan to update this package to have text color functionality?

Actually i tried to change rgba color in this function getDuelTextColor but unfortunately it does't work.

Can you give me a hand in this if you don't plan to update this package soon.

@shqawe1
Copy link
Author

shqawe1 commented Aug 14, 2023

After searching and testing i think i got it to work with rgb and rgba format.

First we should modify options array in Watermark Class in Watermark.php file and we add this option as below:

'fontcolor' => '#ffffff',

Then create new function in same Watermark class as below:

`

    public function setFontColor($fontColor)
        {
            $this->options['fontcolor'] = $this->_colorToRGB($fontColor);
        }

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

Save the file and open ImageCommandBuilder.php file and modify getDuelTextColor function to be like below:

protected function getDuelTextColor() { return [ 'fill "' . $this->options['fontcolor'] . '\ "', "fill \"rgba\\(0,0,0,0.4\\)\"", ]; }

And finally we can use it like this

$watermark->setFontColor('rgba(255,255,255,0)');

I hope it help and thanks for this package it's was really helpful for my project and jzak allah kher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants