메뉴 건너뛰기

Dev tips

PHP [php] imagettftext 사용할때 문자열 정렬하기

taknim 2009.10.22 01:38 조회 수 : 13661 추천:2664

http://www.white-hat-web-design.co.uk/blog/php-captcha-security-images/http://kr.php.net/manual/pt_BR/function.imagettfbbox.php
SIMPLE OVERLOADED FUNCTION: Adds TTF Center and Right Alignment to co ordintate points.
Correctly demonstrates the use of the bouning box TTF function and the array of coordinates that it returns.

After obtaining values and adjusting based on a set of values [L, C, R], it uses simple math based of the length of the text to move it from the origin (x = 0 )

function imagettftextalign($image, $size, $angle, $x, $y, $color, $font, $text, $alignment='L')
{

  
//check width of the text
  
$bbox = imagettfbbox ($size, $angle, $font, $text);
  
$textWidth = $bbox[2] - $bbox[0];
   switch (
$alignment) {
       case
"R":
          
$x -= $textWidth;
           break;
       case
"C":
          
$x -= $textWidth / 2;
           break;
   }

  
//write text
  
imagettftext ($image, $size, $angle, $x, $y, $color, $font, $text);

}
?>