note 77739 added to function.imagettftext
| From: | OleClausen at osu1 dot php dot net | Date: | Tue, 11 Sep 2007 16:11:37 +0000 |
| Subject: | note 77739 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130371@lists.php.net to get a copy of this message | ||
Comment to: Sohel Taslim (03-Aug-2007 06:19)
Thanks for the function which I have modified a bit. In the new version the lines have equal space
between them (the g's in your example create bigger space between the lines) - set by the
parameter '$Leading'.
I have used the for-loop for better performance and slimmed the rest a little :)
/**
* @name : makeImageF
*
* Function for create image from text with selected font. Justify text in image (0-Left,
1-Right, 2-Center).
*
* @param String $text : String to convert into the Image.
* @param String $font : Font name of the text. Kip font file in same folder.
* @param int $Justify : Justify text in image (0-Left, 1-Right, 2-Center).
* @param int $Leading : Space between lines.
* @param int $W : Width of the Image.
* @param int $H : Hight of the Image.
* @param int $X : x-coordinate of the text into the image.
* @param int $Y : y-coordinate of the text into the image.
* @param int $fsize : Font size of text.
* @param array $color : RGB color array for text color.
* @param array $bgcolor : RGB color array for background.
*
*/
function imagettfJustifytext($text, $font="CENTURY.TTF", $Justify=2, $Leading=0, $W=0,
$H=0, $X=0, $Y=0, $fsize=12, $color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){
$angle = 0;
$_bx = imageTTFBbox($fsize,0,$font,$text);
$s = split("[\n]+", $text); // Array of lines
$nL = count($s); // Number of lines
$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W; // If Width not initialized by programmer then it
will detect and assign perfect width.
$H = ($H==0)?abs($_bx[5]-$_bx[3])+($nL>1?($nL*$Leading):0):$H; // If Height not
initialized by programmer then it will detect and assign perfect height.
$im = @imagecreate($W, $H)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, $bgcolor[0], $bgcolor[1], $bgcolor[2]); // RGB
color background.
$text_color = imagecolorallocate($im, $color[0], $color[1], $color[2]); // RGB color text.
if ($Justify == 0){ //Justify Left
imagettftext($im, $fsize, $angle, $X, $fsize, $text_color, $font, $text);
} else {
// Create alpha-nummeric string with all international characters - both upper- and lowercase
$alpha = range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);
// Use the string to determine the height of a line
$_b = imageTTFBbox($fsize,0,$font,$alpha);
$_H = abs($_b[5]-$_b[3]);
$__H=0;
for ($i=0; $i<$nL; $i++) {
$_b = imageTTFBbox($fsize,0,$font,$s[$i]);
$_W = abs($_b[2]-$_b[0]);
//Defining the X coordinate.
if ($Justify == 1) $_X = $W-$_W; // Justify Right
else $_X = abs($W/2)-abs($_W/2); // Justify Center
//Defining the Y coordinate.
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H, $text_color, $font, $s[$i]);
$__H += $Leading;
}
}
return $im;
}
----
Server IP: 86.58.145.50
Probable Submitter: 147.29.108.158
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/77739
Del: integrated -- https://master.php.net/note/delete/77739/integrated
Del: useless -- https://master.php.net/note/delete/77739/useless
Del: bad code -- https://master.php.net/note/delete/77739/bad+code
Del: spam -- https://master.php.net/note/delete/77739/spam
Del: non-english -- https://master.php.net/note/delete/77739/non-english
Del: in docs -- https://master.php.net/note/delete/77739/in+docs
Del: other reasons-- https://master.php.net/note/delete/77739
Reject -- https://master.php.net/note/reject/77739
Search -- https://master.php.net/manage/user-notes.php