<?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);
<?php $w = 400; $h = 200; $image = ImageCreateTrueColor($w, $h); ImageFilledRectangle($image, 0, 0, $w, $h, 0xffffff); $x = 10; $y = 190; $i = 5; $black = 0; $white = 0xfff; $size = 18; //$font = ImagePSLoadFont(__DIR__ . '/odstemplik/odstemplik.otf'); $font = ImagePSLoadFont('Times'); $text = 'Pack my box with five dozen liquor jugs.'; // normal image ImagePSText($image, $text, $font, $size, $black, $white, $x, $y, 0, 0, 0, 4); // extra space between words ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 30, 100, 0, 0, 4); // extra space between letters ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 60, 0, 100, 0, 4);