/** * addText * * @param array options Array contains options * array( * 'text' The string to draw * 'x' Horizontal position * 'y' Vertical Position * 'Color' Font color * 'font' Font to be used * 'size' Size of the fonts in pixel * 'resize_first' Tell if the image has to be resized * before drawing the text * ) * * @return none * @see PEAR::isError() */ function addText($params) { $default_params = array('text' => 'This is Text', 'x' => 10, 'y' => 20, 'color' => array(255, 0, 0), 'font' => 'Arial.ttf', 'size' => '12', 'angle' => 0, 'resize_first' => false); $params = array_merge($default_params, $params); extract($params); if (!is_array($color)) { if ($color[0] == '#') { $this->colorhex2colorarray($color); } else { include_once 'Image/Transform/Driver/ColorsDefs.php'; $color = isset($colornames[$color]) ? $colornames[$color] : false; } } $c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); if ('ttf' == substr($font, -3)) { ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } else { ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } return true; }
/** * addText * * @param array $params Array contains options * array( * 'text' The string to draw * 'x' Horizontal position * 'y' Vertical Position * 'color' Font color * 'font' Font to be used * 'size' Size of the fonts in pixel * 'resize_first' Tell if the image has to be resized * before drawing the text * ) * * @return bool|PEAR_Error TRUE or a PEAR_Error object on error */ function addText($params) { $params = array_merge($this->_get_default_text_params(), $params); extract($params); $options = array('fontColor' => $color); $color = $this->_getColor('fontColor', $options, array(0, 0, 0)); $c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); if ('ttf' == substr($font, -3)) { ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } else { ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); } return true; }
<?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 list($x, $y) = pc_ImagePSCenter($image, $text, $font, $size); ImagePSText($image, $text, $font, $size, $fore, $back, $x, $y);
<?php $font = ImagePSLoadFont('/path/to/font.pfb'); ImagePSText($image, $text, $font, $size, $text_color, $background_color, $x, $y); ImagePSFreeFont($font);
<?php // 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);
<?php $font = ImagePSLoadFont('/path/to/font.pfb'); ImagePSText($image, 'I love PHP Cookbook', $font, $size, $text_color, $background_color, $x, $y);