note 86134 modified in function.imagettfbbox by danbrown

From: Date: Sat, 04 Oct 2008 17:28:15 +0000
Subject: note 86134 modified in function.imagettfbbox by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145058@lists.php.net to get a copy of this message
This is a function which reformats a text string into a text block of a given width. Usefull when you have a long single line string and want to fit it into a fixed width but don't care about it's height <?php function makeTextBlock($text, $fontfile, $fontsize, $width) { $words = explode(' ', $text); $lines = array($words[0]); $currentLine = 0; for($i = 1; $i < count($words); $i++) { $lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $words[$i]); if($lineSize[2] - $lineSize[0] < $width) { $lines[$currentLine] .= ' ' . $words[$i]; } else { $currentLine++; $lines[$currentLine] = $words[$i]; } } return implode("\n", $lines); } ?> --was-- This is a function which reformats a text string into a text block of a given width. Usefull when you have a long single line string and want to fit it into a fixed width but don't care about it's height function makeTextBlock($text, $fontfile, $fontsize, $width) { $words = explode(' ', $text); $lines = array($words[0]); $currentLine = 0; for($i = 1; $i < count($words); $i++) { $lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $words[$i]); if($lineSize[2] - $lineSize[0] < $width) { $lines[$currentLine] .= ' ' . $words[$i]; } else { $currentLine++; $lines[$currentLine] = $words[$i]; } } return implode("\n", $lines); } http://php.net/manual/en/function.imagettfbbox.php

« previous php.notes (#145058) next »