2011-12-30 117 views
0

我使用「xampp-win32-1.7.1-installer」作爲服務器& Dreamwaver cs5進行編碼。我想啓用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) 
?> 

和示值誤差爲:

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

回答

0

您以原始數據輸出圖像。這沒關係,但你也發出HTML代碼,從而破壞你的形象。

首先,您需要將兩者分開,並且這應該會產生您需要假設現有的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 2011-12-30 11:13:03

1

這是因爲你的HTML表單添加到圖像的輸出的頂部。

讓他們單獨的腳本,或將其改成這樣:

<?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 2011-12-30 11:19:57

+0

爲你的禮儀+1;) – Alasdair 2011-12-30 11:29:22

1

你實際發送HTML當您已經發送圖像/ JPEG內容頭?發送圖像內容標題前

<?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清除輸出():

嘗試爲followes。

編輯:糾正小錯誤。

+0

是的,我明白了。感謝您回放@Dennis jamin :) – webrider 2011-12-30 11:14:08

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 2011-12-30 11:10:15

+0

不客氣。快樂的編碼。 – 2011-12-30 12:45:58