<?php

function ImagePSCenter($image, $text, $font, $size, $space = 0, $tightness = 0, $angle = 0)
{
    // find the size of the image
    $xi = ImageSX($image);
    $yi = ImageSY($image);
    // find the size of the text
    list($xl, $yl, $xr, $yr) = ImagePSBBox($text, $font, $size, $space, $tightness, $angle);
    // compute centering
    $x = intval(($xi - $xr) / 2);
    $y = intval(($yi + $yr) / 2);
    return array($x, $y);
}
$image = ImageCreate(500, 500);
$text = 'PHP Cookbook Rules!';
$font = ImagePSLoadFont('/path/to/font.pfb');
$size = 20;
$black = 0x0;
$white = 0xffffff;
list($x, $y) = ImagePSCenter($image, $text, $font, $size);
ImagePSText($image, $text, $font, $size, $white, $black, $x, $y);
ImagePSFreeFont($font);
header('Content-type: image/png');
ImagePng($image);
ImageDestroy($image);
Example #2
0
<?php

$image = ImageCreateTrueColor(1000, 800);
$color = 0xff00;
$string = "this is my first string!";
$path = 'BlockFont.ttf';
//imagestring($image, 5, 100, 200, $string, $color);
imagefttext($image, 50, 0, 200, 400, $color, $path, $string);
//计算出字体占比大小
//$box=imageftbbox(1,10,$path,$string);
//print_r($box);
header('Content-type:image/png');
ImagePNG($image);
ImagePSFreeFont($image);
ImageDestroy($image);