2011-12-30 14 views
0

コーディングにサーバー& Dreamwaver cs5として "xampp-win32-1.7.1-installer"を使用しています。私はPHPのGDサポートを有効にしたい。私は見たPHPでのGDサポート

phpinfo(); 

GDサポートが有効であることを示している。しかし、それはまだ動作しません。なぜそれがうまくいかないのか分からないのですか?私は何をすべきか?

私は実際には、PHPで画像を作成したいと思います。テキストボックス&送信ボタンがあります。私が入力&を押して提出すると、そのイメージボックスに表示されます。それは他の多くのプラットフォームで行うことができますが、今度は私はPHPでそれをしたいです。

は、ここに私のコードです:

<?php 
header("Content-type: image/jpeg"); 
?> 
<form action="Creating_Images_with_PHP.php" method="get"> 
<input type="text" name="name" /> 
    <input type="submit" value="Enter" /> 
</form> 

<?php 
$name = $_GET['name']; 
$message = "Welcome to php academy, $name"; 

$length = strlen($message) * 9.3; 

$image = imagecreate($length, 20); 
$background = imagecolorallocate($image, 0, 0, 0); 
$foreground = imagecolorallocate($image, 255, 255, 255); 

imagestring($image, 5,5,1, $message, $foreground); 

imagejpeg($image) 
?> 

と示すエラーがある:あなたのHTMLフォームは、画像の出力の最上部に追加されているため

"The image http://localhost/www/...blaa blaa blaa cannot be displayed because it contains errors." 

答えて

0

生データとして画像を出力する。そうだけど、あなたもhtmlコードを送信して、あなたのイメージを壊す。

2つを分離する必要があります。これは、既存のPHPコードが必要と仮定して必要なものを生成するはずです。 something.html

<form action="Creating_Images_with_PHP.php" method="get"> 
<input type="text" name="name" /> 
    <input type="submit" value="Enter" /> 
</form> 

Creating_Images_with_PHP.php

<?php 

header("Content-type: image/jpeg"); 

$name = $_GET['name']; 
$message = "Welcome to php academy, $name"; 

$length = strlen($message) * 9.3; 

$image = imagecreate($length, 20); 
$background = imagecolorallocate($image, 0, 0, 0); 
$foreground = imagecolorallocate($image, 255, 255, 255); 

imagestring($image, 5,5,1, $message, $foreground); 

imagejpeg($image) 
?> 

あなたはスクリプトやHTMLコードを作る上で動作することができますことをテストしたら

は、同じファイルに住んでいます。これを行うには、名前欄のリクエスト情報を確認します。

if(isset($_GET['name'][1])){ 
    /* generate image */ 
}else{ 
    /* output form */ 
} 
+0

ありがとうございました。それがうまくいかない理由です。今私はそれを得た。ありがとうcrolpa :) – webrider

1

それはあります。

は、それらの別々のスクリプトを作成し、またはこれに変更します。あなたは、画像/ jpegのコンテンツヘッダを送信した後

<?php 
if (isset($_GET['name']) && $_GET['name']!='') 
{ 
header("Content-type: image/jpeg"); 
$name = $_GET['name']; 
$message = "Welcome to php academy, $name"; 

$length = strlen($message) * 9.3; 

$image = imagecreate($length, 20); 
$background = imagecolorallocate($image, 0, 0, 0); 
$foreground = imagecolorallocate($image, 255, 255, 255); 

imagestring($image, 5,5,1, $message, $foreground); 

imagejpeg($image); 
} 
else 
{ 
echo '<html><body><form action="Creating_Images_with_PHP.php" method="get"> 
<input type="text" name="name" /> 
    <input type="submit" value="Enter" /> 
</form></body></html>'; 
} 
?> 
+0

はい、わかりました。返信をありがとうございました。 :) – webrider

+0

あなたのマナーのために+1;) – Alasdair

1

あなたが実際にHTMLを送信していますか?

としてfollowes試してみてください:出力バッファリング上

<?php 
    ob_start(); 
?> 

<form action="Creating_Images_with_PHP.php" method="get"> 
    <input type="text" name="name" /> 
    <input type="submit" value="Enter" /> 
</form> 

<?php 
    if (isset($_GET['name']) && !empty($_GET['name'])) 
    { 
     ob_clean(); 
     header("Content-type: image/jpeg"); 
     $name = $_GET['name']; 
     $message = "Welcome to php academy, $name"; 

     $length = strlen($message) * 9.3; 

     $image = imagecreate($length, 20); 
     $background = imagecolorallocate($image, 0, 0, 0); 
     $foreground = imagecolorallocate($image, 255, 255, 255); 

     imagestring($image, 5,5,1, $message, $foreground); 

     imagejpeg($image); 
    } 
?> 

この最初のターンをあなたはob_clean使用して出力をオフにすることができますので、()画像コンテンツヘッダを送信する前に。

編集:修正小エラー。

+0

はい、私はそれを得ました。 @Dennis jaminを再生していただきありがとうございます:) – webrider

1

私はあなたのコードを試しました。それは私のためにうまく動作します。

<?php 

if(isset($_GET['name'])) 
{ 
header("Content-type: image/jpeg"); 
$name = $_GET['name']; 
$message = "Welcome to php academy, $name"; 

$length = strlen($message) * 9.3; 

$image = imagecreate($length, 20); 
$background = imagecolorallocate($image, 0, 0, 0); 
$foreground = imagecolorallocate($image, 255, 255, 255); 

imagestring($image, 5,5,1, $message, $foreground); 

imagejpeg($image); 
exit; 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<form action="" method="get"> 
<input type="text" name="name" /> 
    <input type="submit" value="Enter" /> 
</form> 
</body> 
</html> 
+0

多くの多くのありがとう親愛なるプラサド、それは絶対に働いています。 :) :) :) – webrider

+0

あなたはようこそ。ハッピーコーディング。 –