1

わかりましたので、PHP の GD ライブラリを使用して投票や画像などを生成する方法を学習しています。問題は、imagejpeg関数を使用した画像出力に関しては、画面に何も表示されないことです。この演習では、HTML フォーラムにテキストを入力します。これは、GD ライブラリを使用して画像に配置する必要があり、基本的には画像にテキストを配置します。画像が表示されない理由がわかりません。以下は、HTML フォーム コードと PHP コードの両方です。

HTML フォーム:

<html>
    <head> 
        <title> Create Buttons </title> 
    </head>
    <body>
        <form action ="button.php" method ="post">
            <p> Type button text here </p>
            <input type="text" name="button_text" size ="20" />
            <p> Choose button color: </p>
            <input type ="radio" name="color" value="red"> Red <br>
            <input type ="radio" name="color" value="green"> Green <br>
            <input type ="radio" name="color" value="blue"> Blue <br>
            <input type ="submit" value ="Create Button" />
        </form>    
    </body>
</html>

PHP コード:

<?php
Header('Content-type: image/jpeg');
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$button_text = $_POST['button_text']; var_dump($button_text);
$color = $_POST['color']; var_dump($color);
if(empty($color) || empty($button_text)) {
    echo " Could not create image - form not filled correctly";
    exit;
}
$path = $DOCUMENT_ROOT."/uploads/$color-button.jpg";
$im = imagecreatefromjpeg ($path);
$width_image = imagesx($im);
$height_image = imagesy($im);

$width_image_wo_margins = $width_image-(2*18);
$height_image_wo_margins = $width_image-(2*18);

$font_size = 33;
putenv('GDFONTPATH=C:\Windows\Fonts');

$fontname = getenv('GDFONTPATH') . '\comic.ttf';
if(!is_file($fontname)) {
    die( "Missing Font");
}

do {
    $font_size--;
    $bbox = imagettfbbox($font_size,0,$fontname,$button_text);
    $right_text = $bbox[2];
    $left_text = $bbox[0];
    $width_text  = $right_text -$left_text;
    $height_text = abs($bbox[7] - $bbox[1]);
}

while($font_size > 8 && ( $height_text > $height_image_wo_margins || $width_text > $width_image_wo_margins));

if ( $height_text > $height_image_wo_margins || $width_text > $width_image_wo_margins) {
    echo "Text given wil not fit on button.<br />";
} else {
    $text_y = $width_image/2.0 - $width_text/2.0;
    $text_x = $height_image/2.0 - $height_text/2.0;

    if($left_text < 0) {
        $text_x += abs($left_text); //add factor for left overhang.  
    }
    $above_line_text = abs($bbox[7]);
    $text_y += $above_line_text;
    $text_y -=2;
    $white = imagecolorallocate($im,255,255,255);
    imagettftext($im, $font_size, 0, $text_x, $text_y, $white, $fontname, $button_text);

    imagejpeg($im, NULL,75); 
}
imagedestroy($im);
?>
4

3 に答える 3

0

あなたの問題はvar_dump声明です。

それはあなたの出力にテキストを送信しています。呼び出しはおそらく画像を正常に作成していますが、ステートメントimagejpeg()ではレンダリングされません。var_dump

于 2012-12-16T03:49:00.157 に答える
0

画像を表示するために実際に何もしていないため、表示されていません。このスクリプトは画像を作成しますが、通常の HTML と同じように、実際にこれを呼び出すには別のスクリプトが必要です。

このトピックは以前 SO で取り上げられました: PHP GD Library: Output an image and text on same page

また、チュートリアルを 1 つまたは 2 つ実行することをお勧めします。ざっと検索すれば数十件あります。これを「20 の優れた GD チュートリアル」の投稿に変える危険を冒して、ここに投稿することはしません。

于 2012-12-16T02:58:19.373 に答える
0

私のために働いた header('Content-Type: image/jpeg') を使用する場合、echo ステートメントを削除します

于 2012-12-16T03:04:22.917 に答える