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 8213161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:01:04+00:00 2026-06-07T11:01:04+00:00

Simple question, I searched over there and didnt found a way to make this.

  • 0

Simple question, I searched over there and didnt found a way to make this.

I have a string (variable one) which can be like this:

#FF9900Hel#FFFFFFlo

I need a way to make the imagettftext function recognice those colors and draw the text using those colors. For example, in the example I mentioned before, the text should be: in red: Hel and in white:lo. I hope I explained well what I mean.

Thanks in advance

  • 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-07T11:01:05+00:00Added an answer on June 7, 2026 at 11:01 am

    You not only need to recognize colors, you also need to align the strings. This is not automatic as it is in HTML.

    I would do this by first splitting the string into its components parts:

    // Split the text using hex colors as strings:
    $hex     = '[0-9a-fA-F]'; // Capital hex should be supported
    
    $colorex = "(#{$hex}{$hex}{$hex}{$hex}{$hex}{$hex})";
    // or also $colorex = "(#{$hex}{6})";
    
    $parts = preg_split ("/{$colorex}/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    
    // Then you would iterate through the parts:
    
    $color = imageColorAllocate($gd, 0, 0, 0); // Default is black
    foreach($parts as $part)
    {
        // Scan the hex value
        if (preg_match("/^{$colorex}\$/", $part, $gregs))
        {
            sscanf($gregs[1], "#%02x%02x%02x", &$r, &$g, &$b);
            $color = imageColorAllocate($gd, $r, $g, $b);
            continue;
        }
        // IMPROVEMENT: if count(explode("\n", $part)) > 1, $y += $height, $x = 0
        // to indicate "go to next line". So this would appear on two lines:
        //     #012345Hel#ff7700lo,
        //     #443212World 
        // Next section will be repeated for each item
        // in the exploded string.
        //! $subparts = explode("\n", $part);
        //! foreach($subparts as $part)
        //! { // We have overwritten $part
    
        // Here $part is printed as string at position $x, $y
        // Ask GD to calculate string width
        // with http://php.net/manual/en/function.imagettfbbox.php
        // Calculations with $angle != 0 is a bit more difficult, entails trigonometric
        // evaluation of $w and $h.
        $bbox = imagettfbbox ($size, $angle, $fontfile, $part);
        $w    = $bbox[4] - $bbox[0];  // 0,1 is lower left corner
        $h    = $bbox[5] - $bbox[1];  // 4,5 is upper right
        imagettftext ($gd, $size, $angle, $x, $y, $color, $fontfile, $part);
        // Increment $x position by $w
        $x += $w;
    
        //!     if (count($subparts) > 1)
        //!     {
        //!         $x = 0; $y += $h;
        //!     }
        //! }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched all over for an answer to this simple question, with no luck.
I have a very simple question (I searched on google but found nothing). How
This might be a simple question but I've searched and searched and can't find
I searched and searched for an answer for this seemingly simple question in Commerce
i think this is a simple question but I've searched around and can't seem
This is quite a noobish question, I have searched for a few hours now
is there a document property named ssh? It's a simple question. I've seen this
I have a simple small question which someone who knows will be able to
i have a very simple question. i searched for it everywhere but i didn't
I've searched hi and low for an answer to this seemingly simple question, however

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.