php gurus: need help w/ GD text-to-image on-the-fly w/ word-wrap

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • piku
    Senior Member
    • Mar 2004
    • 153

    #1

    php gurus: need help w/ GD text-to-image on-the-fly w/ word-wrap

    trying to achieve this, you can see what its currently doing here

    just wondering if someone already got something like this but running well...? TIA.

    here's the code in the image(.php) on the right side...
    Code:
    <?
    function myWordWrap($txt,$font,$size,$width)
    {
       /*
           word-wrapper.  gets bounding box sizes for each word in a string, then
           strings words together up to desired width. calls strpos and
           imagettfbbox only once per word - so very fast.
           this version reconcatenates the words with a <br> character where
           the line break should be.
       */
       $txt.=" "; // guaranteed to find end of line
       $spaces=array();
       $wids=array();
       $i=0;
       while(true)
       {
           $j=strpos(substr($txt,$i)," ");
           if(!($j===false))
           {
               $spaces[]=$j+$i;
               $bbox=imagettfbbox($size,0,$font,substr($txt,$i,$j+1));
               $left=($bbox[0]>$bbox[6])?$bbox[6]:$bbox[0];
               $right=($bbox[2]>$bbox[4])?$bbox[2]:$bbox[4];
               $wids[]=$right-$left;
               $i=$j+$i+1;
           }
           else break;
       }
       $lastspace=-1;
       $cum=0;
       $t2="";
       for($i=0;$i<count($spaces);$i++)
       {
           if((($cum>0)&&($cum+$wids[$i])>$width)) // time for a line break
           {
               $t2.="<br>";
               $cum=0;
               $i--;
           }
           else
           {
               // we'll always get at least one word (even if too wide) thanks to
               // ($cum>0) test above
               $t2.=substr($txt,$lastspace+1,$spaces[$i]-$lastspace);
               $cum+=$wids[$i];
               $lastspace=$spaces[$i];
           }
       }
       return $t2;
    } // end function myWordWrap()
    
    // check for user input, if none...use default values
    if ( empty($t) ) {
    	$t = 'Custom Font PHP-GD On-the-fly: Haettenschweiler 14px';
    }
    if ( empty($width) ) {
    	$width = 436;
    }
    if ( empty($height) ) {
    	$height = 24;
    }
    if ( empty($font_size) ) {
    	$font_size = 14;
    }
    
    $text = $t;
    
    $img = imagecreate($width, $height);
    $white = imagecolorallocate($img,255,255,255); 
    $black = imagecolorallocate($img,0,0,0);
    $grayF7F7F7 = imagecolorallocate($img,247,247,247);
    $gray333333 = imagecolorallocate($img,51,51,51);
    imagefill($img,0,0,$grayF7F7F7); 
    
    // set font
    $font = array(1 => "trebuchet","myriadweb","haettenschweiler");
    $font_path = 'fonts/'.$font[3].'.ttf';
    
    $textnew = myWordWrap($text,$font_path,$font_size,$width);
    
    // array imagettftext ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text)
    Imagettftext($img,$font_size,0,0,14,$gray333333,$font_path,$textnew);
    
    if (function_exists("imagegif")) {
       header("Content-type: image/gif");
       imagegif($img);
    } elseif (function_exists("imagejpeg")) {
       header("Content-type: image/jpeg");
       imagejpeg($img, "", 80);
    } elseif (function_exists("imagepng")) {
       header("Content-type: image/png");
       imagepng($img);
    } elseif (function_exists("imagewbmp")) {
       header("Content-type: image/vnd.wap.wbmp");
       imagewbmp($img);
    } else {
       die("No image support in this PHP server");
    }
    imagedestroy($img);
    ?>
    Last edited by piku; 04-24-2004, 05:58 PM.
    my signature was deleted. i miss my signature-filled-with-helpful-dathorn-links *sniff*sniff*
  • -Oz-
    Senior Member
    • Mar 2004
    • 545

    #2
    In $font_path, should that be $t instead of $text, sorry, got lost following.

    It looks like on the page its flipping out with an extra ' in there like don't. Maybe try putting $t in " " instead of ' '.

    Myabe i'll take a close look later. Those are my ideas for now.

    Oh yeah:
    Imagettftext($img,$font_size,0,0,14,$gray333333,$f ont_path,$textnew);
    get rid of the space ins font_path.
    Dan Blomberg

    Comment

    • sdjl
      Senior Member
      • Mar 2004
      • 502

      #3
      You do realise there's the wordwrap(); PHP function?

      David
      -----
      Do you fear the obsolescence of the metanarrative apparatus of legitimation?

      Comment

      • piku
        Senior Member
        • Mar 2004
        • 153

        #4
        thats when i copied and pasted in the post...it shouldn't be there in the file...
        Originally posted by -Oz-
        Oh yeah:
        Imagettftext($img,$font_size,0,0,14,$gray333333,$f ont_path,$textnew);
        get rid of the space ins font_path.
        edit: weird, when i edit he quote block it doesn't display the space...hmmm
        my signature was deleted. i miss my signature-filled-with-helpful-dathorn-links *sniff*sniff*

        Comment

        • piku
          Senior Member
          • Mar 2004
          • 153

          #5
          i'll take a look at that--know, or have you come to any functions that will extend the canvas downward by $size when it wraps?

          Originally posted by sdjl
          You do realise there's the wordwrap(); PHP function?

          David
          my signature was deleted. i miss my signature-filled-with-helpful-dathorn-links *sniff*sniff*

          Comment

          • sdjl
            Senior Member
            • Mar 2004
            • 502

            #6
            I can't say i've used many of the image functions to be honest.

            David
            -----
            Do you fear the obsolescence of the metanarrative apparatus of legitimation?

            Comment

            Working...