note 87145 added to function.imagettftext
| From: | roland dot hentschel at t-online dot de | Date: | Fri, 21 Nov 2008 11:53:41 +0000 |
| Subject: | note 87145 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146788@lists.php.net to get a copy of this message | ||
Hi!
I played around with that multi-line-script "makeImageF".
Added some features, mainly: you can read text-files
or add text as parameter - eg :
imagettftext.php?file=test.txt
imagettftext.php?text=hello|world!
have fun!
======================================
<?php header("Content-type: image/png");
/*
* @name: makeImageF
* http://de3.php.net/manual/en/function.imagettftext.php#77739
*
* @param String $file : Textfile.
* @param String $text : Text (New Line : "|").
* @param String $font : Font name.
* @param int $fsize: Font size.
* @param int $pos : Justify (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 array $rgb: RGB color array for text & background color.
*
*/
function rgb2hex($rgb) {
$color = array("black"=>"#000", "grn"=>"#090",
"red"=>"#f00", "blu"=>"00f");
$trans = array("a"=>"10", "b"=>"11",
"c"=>"12",
"d"=>"13", "e"=>"14",
"f"=>"15");
$hex = array();
$rgb = str_replace("#","",
str_replace(array_keys($color), array_values($color), strtolower($rgb)));
for ($x=0; $x<strlen($rgb); $x++)
$hex[] = strtr($rgb[$x],$trans);
if (count($hex)==3) {
$hex[5] = $hex[2]; $hex[4] = $hex[2];
$hex[3] = $hex[1]; $hex[2] = $hex[1];
$hex[1] = $hex[0];
};
$hex = ($hex[0]*16+$hex[1])
."x".($hex[2]*16+$hex[3])
."x".($hex[4]*16+$hex[5]);
return $hex;
}
$file = $_GET['file']
? $_GET['file'] : "logo.txt";
if ($_GET['file']) {
$fp = @fopen($file, "r");
while (!feof($fp)) $text .= fgets($fp); fclose($fp);
} else $text = str_replace("|","\n",$_GET['text']);
$pos = $_GET['pos']
? $_GET['pos'] : 0;
$font = $_GET['font']
? "./fonts/".$_GET['font'].".ttf" : "./fonts/verdana.ttf";
$fsize = $_GET['fsize']
? $_GET['fsize'] : 12;
$rgb = $_GET['rgb']
? $_GET['rgb'] : "#ff9,#393";
imagepng(imagettfpostext($text, $font, $fsize, $pos, $rgb));
function imagettfpostext($text, $font, $fsize, $pos, $rgb,
$Leading=0, $W=0, $H=0, $X=0, $Y=0) {
$angle = 0;
$_bx = imageTTFBbox($fsize,0,$font,$text);
$str = split("[\n]+",$text);
$nL = count($str);
$W = ($W==0)
? abs($_bx[2]-$_bx[0])
: $W;
$H = ($H==0)
? abs($_bx[5]-$_bx[3])+($nL>1
? ($nL*$Leading)
: 0)
: $H;
$im = @imagecreate($W, $H)
or die("Cannot Initialize new GD image stream");
$rgb = explode(",",$rgb);
$tc = explode("x",rgb2hex($rgb[1]));
$bc=explode("x",rgb2hex($rgb[0]));
$bc = imagecolorallocate($im, $bc[0], $bc[1], $bc[2]);
$tc = imagecolorallocate($im, $tc[0], $tc[1], $tc[2]);
if ($pos == 0) {
imagettftext($im, $fsize, $angle, $X,
$fsize, $tc, $font, $text);
} else {
$alpha = range("a", "z");
$alpha = $alpha.strtoupper($alpha).range(0, 9);
$_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, $str[$i]);
$_W = abs($_b[2]-$_b[0]);
if ($pos == 1) $_X = $W-$_W;
else $_X = abs($W/2)-abs($_W/2);
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H,
$tc, $font, $str[$i]);
$__H += $Leading;
}
}
return $im;
}
?>
----
Server IP: 217.13.201.10
Probable Submitter: 62.224.113.80
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/87145
Del: integrated -- https://master.php.net/note/delete/87145/integrated
Del: useless -- https://master.php.net/note/delete/87145/useless
Del: bad code -- https://master.php.net/note/delete/87145/bad+code
Del: spam -- https://master.php.net/note/delete/87145/spam
Del: non-english -- https://master.php.net/note/delete/87145/non-english
Del: in docs -- https://master.php.net/note/delete/87145/in+docs
Del: other reasons-- https://master.php.net/note/delete/87145
Reject -- https://master.php.net/note/reject/87145
Search -- https://master.php.net/manage/user-notes.php