2016-07-05 18 views
-1

텍스트가 "텍스트 정렬 오른쪽"으로 지정하고 싶습니다.PHP : imagettftext 텍스트 정렬 (RTL)

$url = "#"; 
$input = @file_get_contents($url) or die('Fehler!'); 

if(preg_match_all('~<span class="a">\s*(.*?)\s*</span>~si', $input, $item_name)); 

$image = imagecreatefrompng("bg.png"); 
imagesavealpha($image, true); 
imagealphablending($image, true); 
$finalImage = imagecreatetruecolor(800,200); 

$font = '../arial.ttf'; 
$color = imagecolorallocate($finalImage, 0, 0, 0); 
$color_time = imagecolorallocate($finalImage, 100, 100, 100); 

imagettftext($image, 10, 0, 23, 15, $color, $font, $item_name[1][0]); 
imagettftext($image, 10, 0, 22, 33, $color, $font, $item_name[1][1]); 
imagettftext($image, 10, 0, 22, 51, $color, $font, $item_name[1][2]); 
imagettftext($image, 10, 0, 22, 69, $color, $font, $item_name[1][3]); 
imagettftext($image, 10, 0, 22, 87, $color, $font, $item_name[1][4]); 
imagettftext($image, 10, 0, 22, 105, $color, $font, $item_name[1][5]); 
imagettftext($image, 10, 0, 22, 123, $color, $font, $item_name[1][6]); 
imagettftext($image, 10, 0, 22, 141, $color, $font, $item_name[1][7]); 
imagettftext($image, 10, 0, 22, 159, $color, $font, $item_name[1][8]); 
imagettftext($image, 10, 0, 22, 177, $color, $font, $item_name[1][9]); 
imagettftext($image, 10, 0, 22, 195, $color, $font, $item_name[1][10]); 

header('Content-type: image/png'); 
imagepng($image); 

알고 계십니까? 나는 그것을 얻지 않기 때문에. 나는 이미 많은 구글. 너희들은 내가 PHP에 진짜로 들어 있지 않다는 것을 알아야한다. 왜 내가 도움이 필요한지. 질문이 있으시면 문의하십시오! FUNC의

+0

은 당신이 그것을 말해 텍스트를두고 하나님. "왼쪽 정렬"또는 "오른쪽 맞춤"이 없습니다. 직접 구현해야합니다. http://php.net/manual/en/function.imagettfbbox.php –

+0

@MarcB ok, 어떻게? – ZarneXxX

+0

링크를 읽으십시오. 주어진 도구를 사용하는 방법을 배우십시오. "find 함수'do_exactly_what_I_need()'"의 사고 방식에서 빠져 나와'use(); simple_tools(); 제공된(); 달성하기 위해(); 복잡한 _things(); ' –

답변

1

매우 CLEAR 버전, 예에 ....

<?php 

function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) { 
    /************ 
    simple function that calculates the *exact* bounding box (single pixel precision). 
    The function returns an associative array with these keys: 
    left, top: coordinates you will pass to imagettftext 
    width, height: dimension of the image you have to create 
    *************/ 
    $rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text); 
    $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7])); 
    $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7])); 

    return array( 
     "left" => abs($minX) - 1, 
     "top" => abs($minY) - 1, 
     "width" => $maxX - $minX, 
     "height" => $maxY - $minY, 
     "box" => $rect 
    ); 
} 

// Example usage - gif image output 

$text_string = "Hullo World"; 
$font_ttf  = "./fonts/arial.ttf"; 
$font_size  = 22; 
$text_angle  = 0; 
$text_padding = 10; // Img padding - around text 

$the_box  = calculateTextBox($text_string, $font_ttf, $font_size,  $text_angle); 

$imgWidth = $the_box["width"] + $text_padding; 
$imgHeight = $the_box["height"] + $text_padding; 

$image = imagecreate($imgWidth,$imgHeight); 
imagefill($image, imagecolorallocate($image,200,200,200)); 

$color = imagecolorallocate($image,0,0,0); 
imagettftext($image, 
    $font_size, 
    $text_angle, 
    $the_box["left"] + ($imgWidth/2) - ($the_box["width"]/2), 
    $the_box["top"] + ($imgHeight/2) - ($the_box["height"]/2), 
    $color, 
    $font_ttf, 
    $text_string); 

    header("Content-Type: image/gif"); 
    imagegif($image); 
    imagedestroy($image); 

?> 

[기억하지 :. 때문에 헤더의 전 또는 태그 뒤에 공백을,()를 호출하면 로스트! ]

쉬운 텍스트 맞춤을 원한다면 imagettfbbox() 명령을 사용해야합니다. 올바른 매개 변수가 주어지면 배열의 텍스트 필드 경계를 반환하므로 텍스트를 가운데 맞춤 또는 정렬하는 데 필요한 x 및 y 좌표를 계산할 수 있습니다.

수평 센터링 예 :

<?php 
    $tb = imagettfbbox(17, 0, 'airlock.ttf', 'Hello world!'); 
?> 

$ 결핵이 포함됩니다 :

수평 정렬을 위해
Array 
(
    [0] => 0 // lower left X coordinate 
    [1] => -1 // lower left Y coordinate 
    [2] => 198 // lower right X coordinate 
    [3] => -1 // lower right Y coordinate 
    [4] => 198 // upper right X coordinate 
    [5] => -20 // upper right Y coordinate 
    [6] => 0 // upper left X coordinate 
    [7] => -20 // upper left Y coordinate 
) 

, 우리는 "텍스트 상자의"폭 {$ 결핵을 빼줄 필요가있다 [2] 또는 $ tb [4]}를 이미지의 너비에서 뺀 다음 2로 뺍니다.

당신이 200 픽셀 넓은 이미지가 말하는, 당신이 뭔가를 할 수 있습니다 :

<?php 

    $x = ceil((200 - $tb[2])/2); // lower left X coordinate for text 
    imagettftext($im, 17, 0, $x, $y, $tc, '../arial.ttf', 'Hello world!'); // write text to image 
?> 

이것은 제공하거나 1 개 픽셀을, 텍스트 당신에게 완벽한 수평 가운데 정렬을 줄 것이다. 재미있어!

http://php.net/manual/en/function.imagettfbbox.php