سؤال

I'm using imagettftext() to write a letter into an image. The problem is that I don't know what's the letter position inside the image.

enter image description here

As you can see, I drawn border of the image and of the letter using imagettfbbox(), but the letter border is on the left also when the letter is on the right.

So the question is: How can I know where the letter starts (on the x axis)? Every letter looks like having a different "offset" and I just want to know it to draw the black border.

This is the code I used to create the images of the link above:

//$w,$h,$bl(baseline) and $s taken from a text in an html page

$string = "a";
$font = "font/times.ttf";
$h = 247;
$w = 94; //106 for letter "b"
$bl = 54;

header('Content-type: image/png');
$im = imagecreatetruecolor($w, $h);

$back = imagecolorallocate($im, 200, 200, 200);
$asd =  imagecolorallocate($im, 0, 255, 255);

imagefilledrectangle($im, 0, 0, $w, $h, $back);

$x = 0;
$y = $h-$bl;
$s = 212 * 0.75;

imagettftext($im,$s,0,$x,$y,$asd,$font,$string);

$prova = imagettfbbox($s, 0, 'font/times.ttf', $string);
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[2],$h - $bl + $prova[3],imagecolorallocate($im, 0, 0, 0));
imageline($im,$prova[4],$h - $bl + $prova[5],$prova[2],$h - $bl + $prova[3],imagecolorallocate($im, 0, 0, 0));
imageline($im,$prova[4],$h - $bl + $prova[5],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0));
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0));
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[4],$h - $bl + $prova[5],imagecolorallocate($im, 0, 0, 0));
imageline($im,$prova[2],$h - $bl + $prova[3],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0));

imageline($im,0,0,$w,$h,imagecolorallocate($im, 0, 255, 0));
imageline($im,0,$h,$w,0,imagecolorallocate($im, 0, 255, 0));

imagepng($im);
imagedestroy($im);
هل كانت مفيدة؟

المحلول

I just found the reason the in the Manual page of the function: http://www.php.net/manual/en/function.imagettfbbox.php#97357

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top