2016-06-17 5 views
0

imagettftext를 사용하여 PHP로 만든 이미지에서 텍스트를 가운데에 맞출 수있는 방법이 확실하지 않습니다.PHP 이미지의 가운데 맞춤 텍스트

다음
header('Content-Type: image/png'); 
$im = imagecreatetruecolor(260, 180); 
$background = imagecolorallocate($im, 0, 0, 255); 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 60, 153, 181); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 399, 200, $white); 
$text = $_GET['tname']; 
$font = 'OpenSans-Semibold.ttf'; 
imagettftext($im, 20, 0, $x+1, $y+1, $grey, $font, $text); 
imagettftext($im, 20, 0, $x, $y, $black, $font, $text); 
imagepng($im); 
imagedestroy($im); 
+0

사용 [imagetttfbbox] (http://php.net/manual/en/function.imagettfbbox.php) 텍스트의 크기를 계산하려면 상자에서 이미지의 위치를 ​​계산하십시오. –

+0

예제를 제공해 줄 수 있습니까? 나는 설명서를 읽으려고했지만 반환 된 결과에 대한 부분을 정말로 이해하지 못했습니다. – icecreamscoop

답변

1

imagetttfbbox에 따라 동작하는 예제입니다 : 여기에 내 코드입니다

$im = imagecreatetruecolor(200, 75); 
$white = imagecolorallocate($im, 255, 255, 255); 
$black = imagecolorallocate($im, 0, 0, 0); 

// text, font and size to draw 
$text = 'hello world'; 
$font = 'OpenSans-Semibold.ttf'; 
$size = 30; 

// determine the size of the text so we can center it 
$box = imagettfbbox($size, 0, $font, $text); 
$text_width = abs($box[2]) - abs($box[0]); 
$text_height = abs($box[5]) - abs($box[3]); 
$image_width = imagesx($im); 
$image_height = imagesy($im); 
$x = ($image_width - $text_width)/2; 
$y = ($image_height + $text_height)/2; 

// add text 
imagettftext($im, $size, 0, $x, $y, $white, $font, $text); 

// set content type 
header('Content-Type: image/png'); 

// output and destroy image 
imagepng($im); 
imagedestroy($im); 
+0

답장을 보내 주셔서 대단히 죄송합니다. – icecreamscoop

관련 문제