Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate adaptive text that curves inside a circle in php?

Tags:

php

I am trying to create an image with a circle and some text on it where the text curves around the inside of the circle. The circle is adaptive and I want the text inside it to be adaptive.

Until now I have the following code that generates a text that curves:

    $row1="line1";
    $degrees = (130/strlen($row1));
    imageellipse ( $im , 250 , 250 , 390 , 390 , $black );
    imageellipse ( $im , 250 , 250 , 398 , 398 , $black );

    for ($i=0;$i<strlen($row1);$i++) 
    {
    $a = ($degrees*$i)+126-strlen($row1)/3;
    $cos = cos(deg2rad($a));
    $sin = sin(deg2rad($a));
    $x = 0;
    $y = 180;
    $xt = round($cos*($x) - $sin*($y));
    $yt = round($sin*($x) + $cos*($y));
    imagettftext($im,14,180-($a),250+$xt,270+$yt,$red,'fonts\times.TTF',$row1[$i]);
    }

Can you please tell me how can I make it adaptive, so it can adapt with the generated ellipse?

like image 537
Cristina Ursu Avatar asked Nov 12 '22 18:11

Cristina Ursu


1 Answers

Hope this helps:

     $image = imagecreatetruecolor(400,400);
     $white = imagecolorallocate($image,255,255,255);

    imagefill($image,0,0,$white);

    $red     = imagecolorallocate($image,255,0,0);
    $degrees = (360/strlen($text));

        for($i=0;$i<strlen($text);$i++) 
        {
          $a = ($degrees*$i)+180;
          $cos = cos(deg2rad($a));
          $sin = sin(deg2rad($a));
          $x = 0;
          $y = 180;
          $xt = round($cos*($x) - $sin*($y));
          $yt = round($sin*($x) + $cos*($y));

          imagettftext($image,20,180-($a),200+$xt,200+$yt,$red,"C:/WINNT/Fonts
                       arial.ttf",$text[$i]);
        }
like image 73
Joe Brown Avatar answered Nov 15 '22 12:11

Joe Brown