Quote:
Originally Posted by
Vernier
Thanks for the replys guys.
The problem is the separate string lengths will vary depending on there IP or OS.
I don't really want:
Your IP is: 127.0.0.1 Your O
S is: Linux
I need it to be in this format:
Your IP is: 127.0.0.1
Your OS is: Linux
Thanks :)
It seems as if you didn't read Daniel's or my comment.
Use the imagefontheight() function to determine how tall the font is, then separate the strings into two different strings. Call both of the strings through different functions. The second light should have the same y coordinate as the first line PLUS (+) the imagefontheight(). This will not be explained again.
Edit:
If anybody is interested, I've made animagewrapttftext function that will wrap text around the width of an image. Use it like imagettftext. The only difference is that once the text reaches the edge of the image, it wraps.
View an example and its source at http://eg.jfein.net/wraptext/
PHP Code:
function calculateTextBox($size, $angle, $fontfile, $text) { //found on http://php.net/manual/en/function.imagettfbbox.php
$rect = imagettfbbox($size, $angle, $fontfile, $text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX) - 1,
"top" => abs($minY) - 1,
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
function imagewrapttftext($image, $size, $angle, $x, $y, $color, $fontfile, $text) {
if(empty($text)){
return true; // satisfied; job complete.
}
for($i = 0, $relWidth = $x; $i < strlen($text); $i++){
$boundingbox = calculateTextBox($size, $angle, $fontfile, $text[$i]);
$tallestLetter = calculateTextBox($size, $angle, $fontfile, "|");
$relWidth += $boundingbox['width'];
if($relWidth > imagesx($image)){
$tempString = explode(" ", substr($text, 0, $i));
$offsetIdentifier = array_pop($tempString);
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, implode(" ", $tempString));
$newText = substr($text, $i - strlen($offsetIdentifier), strlen($text));
imagewrapttftext($image, $size, $angle, $x, $y + $tallestLetter['height'], $color, $fontfile, $newText);
break;
}
if($i == strlen($text)-1){
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
return true; //satisfied; job complete.
break;
}
}
}
Edit:
A smarter solution, actually, maybe to just explode the string by spaces and test word by word instead of character by character and then back tracing to the last space. If I'm in the mood I'll work on that tomorrow.