Jump to content

wordwrap large chunks of text written with imagettftext() function


dsaba

Recommended Posts

I am writing text with the imagettftext function to an image

The text usually is in paragraph form or in list form.

 

Example of paragraph form:

jfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadf

 

jfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadfjfkdsaljfklsadjfkls;djfkls;djfkldsfasdfsadf

 

Example of list form:

1.fjkslfsdf

2. fjkslfsdf

3. fjkslfsdf

4. fjkslfsdf

 

 

I am writing this text to an image and I want to wordwrap it, where it stops at a certain coordinate and the line breaks.

 

I found these function on the php.net manual for imagettftext

 

<?php

function imageWordWrapBBox ( $Text, $Width = 650, $FontSize = 10, $Font = './fonts/arial.ttf' )
{
    $Words = split ( ' ', $Text );
    $Lines = array ( );
    $Line  = '';

    foreach ( $Words as $Word )
    {
        $Box  = imagettfbbox ( $FontSize, 0, $Font, $Line . $Word );
        $Size = $Box[4] - $Box[0];
        if ( $Size > $Width )
        {
            $Lines[] = trim ( $Line );
            $Line    = '';
        }
        $Line .= $Word . ' ';
    }
    $Lines[] = trim ( $Line );

    $Dimensions = imagettfbbox ( $FontSize, 0, $Font, 'AJLMYabdfghjklpqry019`@$^&*(,' );
    $lineHeight = $Dimensions[1] - $Dimensions[5];

    return array ( $lineHeight, $Lines, $lineHeight * count ( $Lines ) );
}

function imageWordWrap ( $Text, $Width, $Color, $X = 0, $Y = 0, $FontSize = 10, $Font = './fonts/arial.ttf' )
{
    $Data = $this->imageWordWrapBBox ( $Text, $Width, $FontSize, $Font );

    foreach ( $Data[1] as $Key => $Line )
    {
        $locX = $X;
        $locY = $Y + ( $Key * $Data[0] );
        imagettftext ( $this->Image, $FontSize, 0, $locX, $locY, $Color, $Font, $Line );
    }

    return $Data;
}
?> 

 

 

I don't understand how to use it, as there are two functions there in the code and one function uses another function

How do I use these functions what line of code do I need to use it?

I tried using one or both but i keep getting this parse error:

Fatal error: Call to a member function on a non-object

-thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.