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

strokeText is too slow! #57

Open
alikm6 opened this issue Jul 29, 2020 · 0 comments
Open

strokeText is too slow! #57

alikm6 opened this issue Jul 29, 2020 · 0 comments

Comments

@alikm6
Copy link

alikm6 commented Jul 29, 2020

strokeText is very slow for high input (eg 10).

The following code is much more efficient (up to 40 times faster for strokeText = 10)

protected function strokeText($x, $y, $text)
    {
        $size = $this->strokeSize;
        if ($size <= 0) return;

        /*
        // Current algorithm
        for ($c1 = $x - $size; $c1 <= $x + $size; $c1++) {
            for ($c2 = $y - $size; $c2 <= $y + $size; $c2++) {
                $this->drawInternal(new Point($c1, $c2), $this->strokeColor, $text);
            }
        }
        */

        $tmp_box = $this->calculateBox($text);

        $textWidth = $tmp_box->getWidth();
        $textHeight = 1.5 * $tmp_box->getHeight();

        $tmp_img = imagecreatetruecolor($textWidth, $textHeight);
        imagesavealpha($tmp_img, true);
        imagefill($tmp_img, 0, 0, imagecolorallocatealpha($tmp_img, 0, 0, 0, 127));

        imagettftext(
            $tmp_img,
            $this->getFontSizeInPoints(),
            0, // no rotation
            0,
            $tmp_box->getHeight(),
            $this->strokeColor->getIndex($tmp_img),
            $this->fontFace,
            $text
        );

        for ($c1 = $x - $size; $c1 <= $x + $size; $c1++) {
            for ($c2 = $y - $size; $c2 <= $y + $size; $c2++) {
                imagecopy($this->im, $tmp_img, $c1, $c2 - $tmp_box->getHeight(), 0, 0, $textWidth, $textHeight);
            }
        }

        imagedestroy($tmp_img);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant