6

この単純なスクリプトを使用して、テキストから画像を生成しています。

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$text = urldecode($_GET['text']);

$font = 'arial.ttf';

$im = imagecreatetruecolor(400, 30);

$bg_color = imagecolorallocate($im, 255, 255, 255);

$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);

imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);

imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

file.php?text=testing script&color=000000 でスクリプトを呼び出します

今、同じ画像に通常のフォントと太字のフォントが混在するテキストを生成する方法を知りたいです。file.php?text=testing <b>script</b>&color=000000


これを理解するのを手伝ってくれたdqhendricksに感謝します。

これは私が書いた簡単なスクリプトです。まだ多くの改善が必要ですが、基本的な機能についてはうまく機能しているようです:

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$im = imagecreatetruecolor(400, 30);

$white = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 399, 29, $white);

$tmp = $_GET['text'];

$words = explode(" ", $tmp);

$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD

$addSpace = 0;

foreach($words as $word)
{
    if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE

    if(stristr($word, "<b>"))
    {
        $font = 'arialbd.ttf'; // BOLD FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
    }
    else
    {
        $font = 'arial.ttf'; // NORMAL FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
    }

    $addSpace = 1;
}

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

注: これは、スペースで区切られた単一の単語を「太字」にする場合にのみ機能し、単語の一部を太字にする場合には機能しません。

でスクリプトを呼び出しますfile.php?text=testing+<b>script</b>&color=000000

4

2 に答える 2

0

arial-bold フォント ファイルをロードし、使用するフォントごとに 1 つずつ、2 つの別個の imagettftext() 呼び出しを行う必要があります。文字列を解析して太字にする部分と、テキストの各セクションをどこに印刷するかを調べるとなると、非常に複雑なコードになりそうです。あなたはこれを何に使っていますか?同じことを達成するためのより良い解決策があるかもしれません。

添加

imagettftext() 関数からの戻り値を使用して、次のテキスト印刷を開始する場所を決定します。

ドキュメントから:

テキストの境界ボックスを構成する 4 つの点を表す 8 つの要素を持つ配列を返します。ポイントの順序は、左下、右下、右上、左上です。ポイントは角度に関係なくテキストに対して相対的であるため、「左上」とは、テキストを水平方向に見たときに左上隅にあることを意味します。エラー時に FALSE を返します。

于 2011-01-09T01:32:52.883 に答える
-2

応答:

解析は問題になりません。明確でないのは、例の ?text=testing scriptの 2 番目の wordr の X 位置を見つける方法です。つまり、最初の単語がどこで終わるかを知るにはどうすればよいので、2 番目の単語を別の imagettftext() と太字のフォントでそこに配置できますか。— メレディス


誰かが「その答えについては編集を参照してください」と言うのに時間をかけたのは面白いです。

<?PHP
$sentence = "I LOVE giving retarded answers that don't amount to jack!";
$sentence = explode(" ",$sentence );

for($s=0;$s<count($sentence);$s++){

#THERES YOUR INDIVIDUAL WORDS!
$word = $sentence[$s];

}
?>

あなたの X 位置は、あなたのロジックでは単純に $s になります。2 番目の単語を取得するには、次のようにします。

<?PHP
$word1 = $sentence[0];
$word2 = $sentence[1];
$word3 = $sentence[2];
$word4 = $sentence[3];
?>

はい、精神的な視覚効果のためだけに $words に名前を付けました。

$sentence[1] は単語 2 になります

于 2013-03-25T15:53:13.523 に答える