2016-07-05 12 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); 

あなたは知っていますか? 私はそれを得ないので。 私はすでに多くのGoogle。 あなたは知っておく必要があります、私は本当にPHPで得られたわけではない、なぜ私は助けが必要です。 ご質問がある場合は、お問い合わせください! FUNCの

+0

gdは、あなたがそれを伝える場所にのみテキストを置きます。 「左揃え」または「右揃え」がありません。あなたはそれを自分で実装する必要があります:http://php.net/manual/en/function.imagettfbbox.php –

+0

@MarcB ok、どうやって? – ZarneXxX

+0

リンクを読んでください。あなたが与えられたツールを使うことを学ぶ。 "find関数' do_exactly_what_I_need() '"の考え方から抜け出し、 'use();'の考え方に入ります。 simple_tools();提供された();達成するために();複雑なthings(); ' –

答えて

1

非常に明確なバージョン、例と....

<?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 
) 

を、私たちは、[2]または$ "テキストボックスの" 幅{$のTBを減算する必要がありますtb [4]}を画像の幅から差し引き、2を引く。

あなたは200pxの幅の広いイメージを持っていると言って、あなたはこのような何かを行うことができます:

<?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

関連する問題