2017-05-12 2 views
2

私はページに収まるように文字列の配列を持っています。イメージの特定の部分の中央にあるPHP GDの位置配列テキスト

 $imgWidth = 240; 
    $imgHeight = 900; 

    $IMG = imagecreatetruecolor($imgWidth,$imgHeight); 
    $font_type_bold = '/dejavu/DejaVuSansCondensed-Bold.ttf'; 
    $background = imagecolorallocate($IMG, 78,129,154); 
    $text_white = imagecolorallocate($IMG, 255,255,255); 
    $IMG = imagecreatetruecolor($imgWidth,$imgHeight); 
    $max_lenght_of_title = 15; 
    $special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame"; 
    $text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title)); 
    $line_height=30; 
    imageline($IMG, 0, 500, 240, 500, $text_white); 
    imageline($IMG, 0, 100, 240, 100, $text_white); 
    for($i=0;$i<count($text);$i++) { 
     $font_size_si_name_horizontal = 21; 
     //Center the text 
     $size = imagettfbbox(20, 0, $font_type_bold, $text[$i]); 
     $long_text = $size[2] + $size[0]; 
     $posx = ($imgWidth - $long_text)/2; 

     imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, 150+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]); 
    } 
    header("Content-type: image/png"); 
    imagepng($IMG); 
    imagecolordeallocate($IMG, $text_color); 
    imagecolordeallocate($IMG, $background); 

結果がこれであると私は例えば1

enter image description here

を選択し、それはページの特定の部分にする必要があり、どのように私はしないようにこれを行うことができます固定真ん中が、adjustiveこと1つはページのどの部分がテキストになるかによって異なります。

注:テキストは長くなる可能性があるため、主な問題です。テキストのな長さに応じて、タイトルは私が$vert = ($imgHeight/5);を追加し、動的calculted垂直Yのインデックスに対してimagettftext()に入れている途中

+0

あなたは垂直方向に真ん中に表示されるテキストについて言っていますか。 –

+0

はい、垂直の真ん中ですが、全体の高さの中間ではなくページの一部です。マークされた部分の真ん中 – Strahinja90bg

+0

は($新しい、$の画像は、$ margin_x、0、0、0、$ new_w、$ new_h、$幅、$高さ)imagecopyresampledこの \t \t \t \t \tで試してみてください。 \t \t \t \t \t imagejpeg($ new、$ save、100); – CyberAbhay

答えて

0

私は、私は1つのアレイサイズとテキストがなるポイントを調節し、その後、私はちょうど、すべてのための21のフォントサイズによって削除$のmiddle_of_pageの一部では、これ

$middle_of_page = 350; 
for($i=0;$i<count($text);$i++) { 
    $middle_of_page = $middle_of_page - 21; 
} 
imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, $middle_of_page+$line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]); 

を解決する方法を考え出しました$ iは文字列の配列サイズを表します。

enter image description here

0
<?php 
    $imgWidth = 240; 
    $imgHeight = 900; 
    $IMG = imagecreatetruecolor($imgWidth,$imgHeight); 
    $font_type_bold = 'BioRhyme/BioRhyme-Regular.ttf'; 
    $background = imagecolorallocate($IMG, 78,129,154); 
    $text_white = imagecolorallocate($IMG, 255,255,255); 

    $IMG = imagecreatetruecolor($imgWidth,$imgHeight); 
    $max_lenght_of_title = 15; 
    $special_issue_name_mod="This is some example text to be put on the page and split into array to fit he width of frame"; 
    $text = explode("\n", wordwrap($special_issue_name_mod, $max_lenght_of_title)); 
    $line_height=30; 
    $vert = ($imgHeight/5); 
    for($i=0;$i<count($text);$i++) { 
     $font_size_si_name_horizontal = 21; 
     //Center the text 
     $size = imagettfbbox(20, 0, $font_type_bold, $text[$i]); 
     $long_text = $size[2] + $size[0]; 
     $posx = ($imgWidth - $long_text)/2; 

     imagettftext($IMG, $font_size_si_name_horizontal, 0, $posx - 5, $vert+ $line_height + $line_height * $i , $text_white, $font_type_bold, $text[$i]); 
    } 
    header("Content-type: image/png"); 
    imagepng($IMG); 
    imagecolordeallocate($IMG, $text_color); 
    imagecolordeallocate($IMG, $background); 
?> 

にする必要があります!

+0

これは役に立ちませんでした。あなたの方法は、テキストが長いか短い文字列の場合は、1つの可能性のみです。 – Strahinja90bg

+0

ここに示したように、コードには出力が含まれていません。赤い枠線と背景色の異なるブロックが出力されないことを意味します。まず、それを実装して更新します。 –

+0

これは説明のためだけであったので、問題を理解できるように更新しました。 – Strahinja90bg

関連する問題