2016-07-05 81 views
-1

我希望Text是“Text Align Right”。PHP:imagettftext text align right(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,这就是为什么我需要帮助。 如果您有问题,请询问!

+0

gd只将文字放在您告诉它的位置。它没有“左对齐”或“右对齐”。你需要自己实现:http://php.net/manual/en/function.imagettfbbox.php –

+0

@MarcB好的,以及如何? – ZarneXxX

+0

阅读链接。学会使用你得到的工具。退出“查找函数'do_exactly_what_I_need()'”的思维模式,并进入use()的思维模式。 simple_tools();提供();去完成(); complicated_things();' –

回答

1

非常清晰版FUNC,具有例如....

<?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!'); 
?> 

$ TB将包含:

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 
) 

对于水平取向,我们需要。减去了 “文本框的” 宽度{$ TB [2]或$ tb [4]}从图像的宽度,然后减去两个。

说你有一个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