function text2etc($text, $CP, $mode = "png", $trans = 1) { global $TTF_LOCATION, $FONT_SIZE; //$outputtext = implode('',file($f)); $outputtext = $text; $outputtext = Conv2UTF8($outputtext, 1, $CP); $outputtext = str_replace("\r\n", "\n", $outputtext); $outputtext = str_replace("\r", "\n", $outputtext); $outputtext = str_replace("\n", "\r\n", $outputtext); $outputtext = str_replace("<br />", "\r\n", $outputtext); $outputtext = str_replace(" ", " ", $outputtext); $outputtext = unhtmlentities($outputtext); if ($mode == "png") { $dim = imageftbbox($FONT_SIZE, 0, $TTF_LOCATION, $outputtext, array("linespacing" => 1.0)); # $dim= imagettfbbox($FONT_SIZE, 0, $TTF_LOCATION, $outputtext); $min_x = min($dim[0], $dim[2], $dim[4], $dim[6]); $max_x = max($dim[0], $dim[2], $dim[4], $dim[6]); $width = $max_x - $min_x + 1; $min_y = min($dim[1], $dim[3], $dim[5], $dim[7]); $max_y = max($dim[1], $dim[3], $dim[5], $dim[7]); $height = $max_y - $min_y + 1; $img = imagecreate($width + 1, $height + 1); $white = ImageColorAllocate($img, 255, 255, 255); if ($trans) { $twhite = imagecolortransparent($img, $white); } $black = ImageColorAllocate($img, 0, 0, 0); # ImageTTFText($img, $FONT_SIZE, 0, -$min_x+$dim[0],-$min_y, $black, $TTF_LOCATION, $outputtext); ImageFTText($img, $FONT_SIZE, 0, -$min_x + $dim[0], -$min_y, $black, $TTF_LOCATION, $outputtext, array("linespacing" => 1.0)); Header("Content-type: image/png"); ImagePng($img); ImageDestroy($img); } else { if ($mode == "pre") { echo "<pre>\n{$outputtext}\n</pre>"; } else { if ($mode == "text") { Header("Content-type: text/plain"); echo utf8Encode($outputtext); } } } }
$yi = ImageSY($image); // find the size of the text $box = ImageFTBBox($size, $angle, $font, $text, $extrainfo); $xr = abs(max($box[2], $box[4])); $yr = abs(max($box[5], $box[7])); // compute centering $x = intval(($xi - $xr) / 2); $y = intval(($yi + $yr) / 2); return array($x, $y); } $_GET['text'] = 'I <3 PHP!'; // Configuration settings $image = ImageCreateFromPNG(__DIR__ . '/button.png'); $text = $_GET['text']; $font = '/Library/Fonts/Hei.ttf'; $size = 24; $color = 0x0; $angle = 0; // Print-centered text list($x, $y) = ImageFTCenter($image, $size, $angle, $font, $text); ImageFTText($image, $size, $angle, $x, $y, $color, $font, $text); // Preserve Transparency ImageColorTransparent($image, ImageColorAllocateAlpha($image, 0, 0, 0, 127)); ImageAlphaBlending($image, false); ImageSaveAlpha($image, true); // Send image header('Content-type: image/png'); ImagePNG($image); // Clean up ImagePSFreeFont($font); ImageDestroy($image);
<?php $image = ImageCreateTrueColor(200, 50); ImageFilledRectangle($image, 0, 0, 199, 49, 0xffffff); // white $size = 20; $angle = 0; $x = 20; $y = 35; $text_color = 0x0; // black $text = 'Hello PHP!'; $fontpath = __DIR__ . '/stocky/stocky.ttf'; ImageFTText($image, $size, $angle, $x, $y, $text_color, $fontpath, $text); header('Content-type: image/png'); ImagePNG($image);
<?php ImageFTText($image, $size, 0, $x, $y, $text_color, '/path/to/font.ttf', 'I love PHP Cookbook');