Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:29:58+00:00 2026-06-09T22:29:58+00:00

Today I want to make a function that parses the text into lines, guided

  • 0

Today I want to make a function that parses the text into lines, guided by imageTTFBbox(). I created this piece of code but it’s limited to 2 lines. I want to make the same, but in infinite lines. Thanks for your help! 😀

function printe($image, $image_width, $string, $font_size, $y, $color, $font){
    $font = "fonts/" . $font . ".ttf";

    $limit = $image_width - 20;
    $tsize = @imageTTFBbox($font_size,0, $font, $string);
    $twidth = abs($tsize[4] - $tsize[0]);

    $words = explode(" ", $string);
    $text = ''; $text1 = '';$a = 0;$o = 0;

    for($i = 0; $i < count($words); $i++){          
        $tsize = @imageTTFBbox($font_size,0, $font, $words[$i]." ");
        $twidth = abs($tsize[4] - $tsize[0]);
        $nw = $twidth + $a;
        if($nw > $limit OR $o > 0){
            $o++;   
            $text1 .= $words[$i]." ";
        }else{
            $text .= $words[$i]. " ";
            $a =$a+$twidth;
        }
    }

    $txtcolor   = processColor($color); 

    $t1size = @imageTTFBbox($font_size,0, $font, $text);
    $t1width = abs($t1size[4] - $t1size[0]);

    $t2size = @imageTTFBbox($font_size, 0, $font, $text1);
    $t2width = abs($t2size[4] - $t2size[0]);

    $center = ceil($image_width / 2);

    $y1 = $y; $y2 = $y; 

    $xcord1 = ($image_width/2)-($t1width/2)+3;
    $xcord2 = ($image_width/2)-($t2width/2)+3;

    $y2 = $y + ($font_size * 1.5);


    imagettftext($image, $font_size, 0, $xcord1, $y1, $txtcolor, $font, $text);
    imagettftext($image, $font_size, 0, $xcord2, $y2, $txtcolor, $font, $text1);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:29:59+00:00Added an answer on June 9, 2026 at 10:29 pm

    Even if not fully tested, I wrote this code starting from an example found on the PHP manual:

    <?php
    
    function write_multiline_text($image, $font_size, $color, $font, $text, $start_x, $start_y, $max_width) { 
        //split the string 
        //build new string word for word 
        //check everytime you add a word if string still fits 
        //otherwise, remove last word, post current string and start fresh on a new line 
        $words = explode(" ", $text); 
        $string = ""; 
        $tmp_string = ""; 
    
        for($i = 0; $i < count($words); $i++) { 
            $tmp_string .= $words[$i]." "; 
    
            //check size of string 
            $dim = imagettfbbox($font_size, 0, $font, $tmp_string); 
    
            if($dim[4] < ($max_width - $start_x)) { 
                $string = $tmp_string; 
                $curr_width = $dim[4];
            } else { 
                $i--; 
                $tmp_string = ""; 
                $start_xx = $start_x + round(($max_width - $curr_width - $start_x) / 2);        
                imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string); 
    
                $string = ""; 
                $start_y += abs($dim[5]) * 2; 
                $curr_width = 0;
            } 
        } 
    
        $start_xx = $start_x + round(($max_width - $dim[4] - $start_x) / 2);        
        imagettftext($image, $font_size, 0, $start_xx, $start_y, $color, $font, $string);
    }
    
    // Create a 300x300 image
    $im = imagecreatetruecolor(300, 300);
    $black = imagecolorallocate($im, 0, 0, 0);
    $white = imagecolorallocate($im, 255, 255, 255);
    
    // Set the background to be white
    imagefilledrectangle($im, 1, 1, 298, 298, $white);
    
    // Path to our font file
    $font = 'c:/windows/fonts/arial.ttf';
    
    $text = "This is a very ";
    $text .= "long long long long long long long long long long long long long long long long ";
    $text .= "long long long long long long long long long long long long long long long long ";
    $text .= "line of text";
    
    write_multiline_text($im, 12, $black, $font, $text, 10, 22, 298);
    
    // Output to browser
    header('Content-Type: image/png');
    
    imagepng($im);
    imagedestroy($im);
    
    ?>
    

    And here is the resulting output image:

    enter image description here

    If you don’t want the text to be centered, you must substitute the variable $start_xx with $start_x in the two calls to the function imagettftext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question for today is: How can I make function that could find all
I currently have this code. function outputCalendarByDateRange($client, $startDate=2011-06-22, $endDate='2011-06-26') I want $startDate and $endDate
I have a text e.g text<- i am happy today :):) I want to
Before I ask my question, I want to mention that I am only today
This is for a homework assignment, so I don't want the exact code, but
I want to make a SQL query that gets todays date and the most
just joined today loved this site already. My Question is that. i am trying
Yesterday I have made paperclip multiple upload gallery. Today I want to customize this
I want to select data between 1 week ago data until today data: SELECT
today i hit a really annoing problem with wpf. i just want to align

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.