Voting

: five plus four?
(Example: nine)

The Note You're Voting On

god at in-heaven dot org
18 years ago
Here's a simple function for creating an aligned string which is cutted to match the space between $x1 and $x2
<?php
function imagestringcutted($img,$font,$y,$x1,$x2,$text,$color,$align="center") {
$fontwidth = imagefontwidth($font);
$fullwidth = strlen($text) * $fontwidth;
$maxwidth = $x2-$x1;
$targetwidth = $fullwidth-(4*$fontwidth);
if(
$fullwidth > $maxwidth) {
for(
$i = 0; $i < strlen($text) AND ((strlen($text)-($i-4))*$fontwidth) > $targetwidth ;$i++) { }
$text = substr($text,0,(strlen($text)-$i)-4)."...";
}
if(
$align == "left") imagestring($img,$font,$x1,$y,$text,$color);
elseif(
$align == "right") imagestring($img,$font,$x2 - ((strlen($text) * $fontwidth)),$y,$text,$color);
else
imagestring($img,$font,($x2-$x1)/ 2 - strlen($text) * $fontwidth / 2,$y,$text,$color);
}
?>
Usage:
<?php
imagestringcutted
($img,$font,$y,$x1,$x2,$text,$color,$align);
?>
Will create a string $text, which is cutted if it's too long to match between $x1 and $2, on $img with font $font and color $color at height $y and with align to $align.
Hope it will help some people.
Sorry for my bad English.

<< Back to user notes page

To Top