Trying to center text

deadimp

New Member
Messages
249
Reaction score
0
Points
0
I'm not entirely sure what was happening in your code, because what I have here seems to work for me. What I ended up doing is modifying it, and placing the suggestions I had made in there. I also measured out the box on the image, just for kicks. I didn't use 'apache_pb.png', 'cause I don't have access to it.
Changes:
PHP:
function center_ttf($box,$text,$font,$font_size) {
 $size=imagettfbbox($font_size,0,$font,$text);
 $out=new stdClass();
 $out->w=$size[4]-$size[0];
 $out->h=$size[5]-$size[1];
 $out->x=$box->x+($box->w-$out->w)/2;
 $out->y=$box->y+($box->h-$out->h)/2;
 global $image;
 //The 'debugging'
 $color=imagecolorallocate($image,255,0,0);
imagefilledrectangle($image,$out->x,$out->y,$out->x+$out->w,$out->y+$out->h,$color);
 return $out;
}
$string = $_GET['id'];
$image = imagecreatefrompng("img1.png");
//addimage("apache_pb.png",$image,2,3,1,1,76,71);
$font="vera.ttf";
$font_size=16;
$box=(object)array('x'=>86,'y'=>22,'w'=>140,'h'=>30);
$pos=center_ttf($box,$query['display'],$font,$font_size);
write_text($image,$font_size,$pos->x,$pos->y,$query['display'],"FFFFFF",$font);
imagepng($image);
imagedestroy($image);
I'm sorry I had to use that object cast along with the stdClass, but I like my OOP (I'm not too fond of frequently using subscripts).
 
Last edited:
Top