Sympl Logo

Intership @ Sympl Week 13

Wrapping Text & PHP Laravel

Mon 2 - Fri 5 May:

The Final Hiccup

There was one more small issue with the video. If a phrase on the video exceeded the width of the video it would just be cut off. I had to come up with a way to detect the width of the video and put the text on a new line if the width was exceeded. I also needed the solution to take the font type into account because not all font types are created equally. Some are more wide than others.

The Final Solution

After some research I wrote following code that would take the font type, font size, text and maximal width as parameters and return the text with new lines if the width was exceeded.

function wrapText($text, $maxWidth, $fontSize, $fontPath) {
        $words = explode(' ', $text);
        $lineWidth = 0;
        $wrappedText = "";

        foreach ($words as $word) {
            $box = imagettfbbox($fontSize, 0, $fontPath, $word);
            $wordWidth = $box[2] - $box[0];

            if ($lineWidth + $wordWidth < $maxWidth) {
                $wrappedText .= " " . $word;
                $lineWidth += $wordWidth;
            } else {
                $wrappedText .= "\n" . $word;
                $lineWidth = $wordWidth;
            }
        }

        return $wrappedText;
    }

The Next Step

Now that I had a working program with all bugs fixed I gave a demo to the full sympl team. All of them really like the software I wrote and agreed to implement it into the sympl software.

The sympl backend was mainly in PHP Laravel, which I had never worked with before. I was given a week to learn the basics of PHP Laravel and explore the sympl backend to get familiar with it.

By Aron Claessens