이미지 처리 및 GD imagettfbbox

(PHP 4, PHP 5, PHP 7, PHP 8)

imagettfbbox — TrueType 글꼴을 사용하여 텍스트의 경계 상자 제공


설명

imagettfbbox(
    float $size,
    float $angle,
    string $font_filename,
    string $string,
    array $options = []
): array|false
                

이 함수는 TrueType 텍스트의 경계 상자를 픽셀 단위로 계산하여 반환합니다.

메모: PHP 8.0.0 이전에 imageftbbox()extrainfo를 추가로 지원하는 imagettfbbox()의 확장된 변형이었습니다. PHP 8.0.0부터 imagettfbbox()imageftbbox()의 별칭입니다.


매개변수

size
글꼴 크기(포인트).
angle
string이 측정될 각도(도)입니다.
fontfile
사용하려는 트루타입 글꼴의 경로입니다.

PHP가 사용하는 GD 라이브러리 버전에 따라 fontfile이 /로 시작하지 않으면 .ttf가 파일 이름에 추가되고 라이브러리는 라이브러리 정의 글꼴 경로를 따라 해당 파일 이름을 검색하려고 시도합니다.

2.0.18보다 낮은 버전의 GD 라이브러리를 사용할 때 세미콜론이 아닌 공백 문자가 다른 글꼴 파일의 '경로 구분자'로 사용되었습니다. 이 기능을 의도하지 않게 사용하면 경고 메시지가 나타납니다. Warning: Could not find/open font. 이러한 영향을 받는 버전의 경우 유일한 솔루션은 글꼴을 공백이 포함되지 않은 경로로 이동하는 것입니다.

글꼴이 사용하는 스크립트와 동일한 디렉토리에 있는 많은 경우에 다음 트릭을 사용하면 포함 문제를 완화할 수 있습니다.

                      
<?php
// Set the environment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
                      
                    

메모: open_basedirfontfile에 적용되지 않습니다.

string
측정할 문자열입니다.

반환 값

imagettfbbox()는 성공 시 텍스트의 경계 상자를 만들고 오류 시 false을 만드는 4개의 점을 나타내는 8개의 요소가 있는 배열을 반환합니다.

key contents
0 lower left corner, X position
1 lower left corner, Y position
2 lower right corner, X position
3 lower right corner, Y position
4 upper right corner, X position
5 upper right corner, Y position
6 upper left corner, X position
7 upper left corner, Y position

점은 angle에 관계없이 텍스트를 기준으로 하므로 "upper left"는 왼쪽 상단 모서리에서 텍스트를 가로로 보는 것을 의미합니다.


변경 로그

버전 설명
8.0.0 options이 추가되었습니다.

Examples

예제 #1 imagettfbbox() 예제

                  
<?php
// Create a 300x150 image
$im = imagecreatetruecolor(300, 150);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Set the background to be white
imagefilledrectangle($im, 0, 0, 299, 299, $white);

// Path to our font file
$font = './arial.ttf';

// First we create our bounding box for the first text
$bbox = imagettfbbox(10, 45, $font, 'Powered by PHP ' . phpversion());

// This is our cordinates for X and Y
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 25;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// Write it
imagettftext($im, 10, 45, $x, $y, $black, $font, 'Powered by PHP ' . phpversion());

// Create the next bounding box for the second text
$bbox = imagettfbbox(10, 45, $font, 'and Zend Engine ' . zend_version());

// Set the cordinates so its next to the first text
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// Write it
imagettftext($im, 10, 45, $x, $y, $black, $font, 'and Zend Engine ' . zend_version());

// Output to browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>
                  
                

메모

참고: 이 함수는 PHP가 freetype 지원(--with-freetype-dir=DIR)으로 컴파일된 경우에만 사용할 수 있습니다.


기타

  • imagettftext() - TrueType 글꼴을 사용하여 이미지에 텍스트 쓰기
  • imageftbbox() - freetype2를 통해 글꼴을 사용하여 텍스트의 경계 상자 제공