Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2002
    Location
    oc cali
    Posts
    42

    PHP: creating image-text on-the-fly that wraps

    i'm playing around with the idea of having a set width canvas that can handle how many ever text/copy. so far i got this...
    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;
    }
    
    function createImage($txt,$font_path,$font_size,$width,$height) {
    	$img = imagecreate($width, $height); //create canvas
    	$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); 
    	Imagettftext($img,$font_size,0,0,14,$gray333333,$font_path,$txt);
    	if (imagetypes() & IMG_GIF) {
    	   header("Content-type: image/gif");
    	   imagegif($img);
    	} elseif (imagetypes() & IMG_JPG) {
    	   header("Content-type: image/jpeg");
    	   imagejpeg($img, "", 80);
    	} elseif (imagetypes() & IMG_PNG) {
    	   header("Content-type: image/png");
    	   imagepng($img);
    	} elseif (imagetypes() & IMG_WBMP) {
    	   header("Content-type: image/vnd.wap.wbmp");
    	   imagewbmp($img);
    	} else {
    	   die("No image support in this PHP server");
    	}
    	imagedestroy($img);
    }
    
    /****************************/
    
    $font = array(1 => "trebuchet","myriadweb","haettenschweiler");
    $font_path = 'fonts/'.$font[3].'.ttf';
    $width = 436; //width pixels
    $height = 24; //height in pixels
    $font_size = 14; //font-size in pixels
    
    if ( empty($t) ) {
    	$t = 'Custom Font PHP-GD On-the-fly: Haettenschweiler 14px';
    }
    //$txt = myWordWrap($t,$font_path,$font_size,$width);
    $txt = $t;
    
    //echo $txt;
    createImage($txt,$font_path,$font_size,$width,$height);
    ?>
    but the thing that i'm trying to do now is that if i enter anything for $t (or my query string), it will automatically extend the canvas downwards to accompany the characters that are going to be outputted. i know that its going to have to check the width, and enter a EOL or something to wrap the text...then extend the canvas. any help would be great. TIA.

  2. #2
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    If I were doing this I would cheat and use intermediaries. I would probably use latex or docbook to create a ps or pdf file that I would then convert to an image using imagemagick. Yeah, it would be slow, but it would look good and the coding would be easy.

  3. #3
    Join Date
    Aug 2002
    Location
    oc cali
    Posts
    42
    say what? ^^ seems that i haven't touched those areas of php. care to provide a working ex.?

  4. #4
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    All you'd be using php for in this case is to run other programs in a series. I've got a small example of this sort of thing at http://www.flaminglunchbox.net/artic...ddingMath.html. It's not wrapping text, but is instead creating images of equations, but the chain of commands should be the same.

    You would create a latex file. Run latex to create a dvi. Convert the dvi to pnm. Strip the excess white space off of the pnm. Convert pnm to end format.

    This most likely doesn't help you at all. It would be too slow for generating truly dynamic content. It also requires a hefty infrastructure to already be in place.

  5. #5
    Join Date
    Aug 2002
    Location
    oc cali
    Posts
    42
    yah that might help ^^ ...but thanks for giving examples of what we can do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •