note 87145 modified in function.imagettftext by danbrown

From: Date: Fri, 21 Nov 2008 20:27:24 +0000
Subject: note 87145 modified in function.imagettftext by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146810@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 /* * @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. * */ $type = $_REQUEST['type'] ? $_REQUEST['type'] : "png"; switch($type) { case "gif" : header("Content-type: image/gif"); imageGIF(imagettfpostext($text, $font, $fsize, $pos, $rgb),"wm.gif", 9); break; case "png" : header("Content-type: image/png"); imagePNG(imagettfpostext($text, $font, $fsize, $pos, $rgb),"wm.png", 9); break; } 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; } } if ($_REQUEST["trans"]) { $bg_color = ImageColorAt($im,1,1); ImageColorTransparent($im, $bg_color); } return $im; } ?> --was-- 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; } ?> http://php.net/manual/en/function.imagettftext.php

« previous php.notes (#146810) next »