2014-02-18 77 views
1

我的代碼: 將此文件保存爲captcha.php無法獲得GD圖像顯示

<?php 
ob_start(); 
session_start(); 
if(!isset($_POST['submit'])){ 
    echo "<form method=\"post\" action=\"captcha.php\">\n"; 
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; 
    echo "<tr><td>Type The Letters You See Below Into the Box</td></tr>\n"; 
    echo "<tr><td align=\"center\"><img src=\"image.php\"></td></tr>\n"; 
    echo "<tr><td align=\"center\"><input type=\"text\" name=\"image\"></td></tr>\n"; 
    echo "<tr><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Check CAPTCHA\"></td></tr>\n"; 
    echo "</table></form>\n"; 
}else { 
    $image = $_POST['image']; 
    if ($image == $_SESSION['string']) 
    { 
     echo "<b>Great success!</b>\n"; 
    } 
    else 
    { 
     echo "<em>Failure!</em>\n"; 
    } 
} 
ob_end_flush(); 
?> 

這個文件保存爲image.php

<?php 

session_start(); 
$img = imagecreatetruecolor(118,80); 

$white = imagecolorallocate($img, 255, 255, 255); 
$black = imagecolorallocate($img, 0, 0, 0); 
$red = imagecolorallocate($img, 255, 0, 0); 
$grey = imagecolorallocate($img,150, 150, 150); 
$pink = imagecolorallocate($img, 200, 0, 150); 
$yellow = imagecolorallocate($img, 255, 246, 0); 

function randomString($length){ 
    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $str = ""; 
    $i = 0; 

     while($i <= $length){ 
      $num = rand() % 33; 
      $tmp = substr($chars, $num, 1); 
      $str = $str . $tmp; 
      $i++; 
     } 
    return $str; 
} 

for($i=1;$i<=rand(1,5);$i++){ 
    $color = (rand(1,2) == 1) ? $pink : $red; 
    imageline($img,rand(6,90),rand(6,40), rand(6,90)+6,rand(6,40)+6, $color); 
} 

imagefill($img, 0, 0, $yellow); 

$string = randomString(rand(4,6)); 
$_SESSION['string'] = $string; 

imagettftext($img, 20, 10, 10, 52, $black, "calibri.ttf", $string); 
imagettftext($img, 20, 10, 12, 53, $grey, "calibri.ttf", $string); 

header("Content-type: image/png"); 
imagepng($img); 
imagedestroy($img); 

?> 

去這個地址http://www.cufonfonts.com/en/font/12048/calibri和下載字體並將其保存在與我們剛剛創建的.php文件相同的根文件夾中。

注意!這將工作在本地主機上,但IM有一個問題與image.php文件顯示如下 - > JFIF > CREATOR:gd-jpeg v1.0(使用IJG JPEG v90),默認質量 C $「。 「,#(7),01444'9 = 82 <.342 C2 !! 2222222222222222222222222222222222222222222222222222x」 }!1AQa「q2 #B R $3br 。

這只是當我嘗試並將其添加到我創建的CMS時。我需要知道爲什麼這是發生了我有一種感覺它的一些事情做的事情「header(」Content-type:image/png 「);」雖然我可能會出錯,請幫助我,順便說一下,我正在嘗試添加電子郵件表格並通過驗證碼進行評估

這是我嘗試將代碼插入到的表單: 你可以將它保存爲form.php

<?php 
$query = mysql_query("SELECT * FROM forms where id = '$formid'"); 

echo '<h2>'.mysql_result($query, 0, "title").'</h2>'; 

if(mysql_result($query, 0, "description") != "") { 
    echo '<p>'.mysql_result($query, 0, "description").'</p>'; 
} 
error_reporting(E_ERROR); 

?> 
<form action="plugins/form_send.php" method="post" enctype="multipart/form-data" class="ajax" name="formplugin" id="form_<?php echo $formid; ?>"> 
<input name="formid" id="formid" type="hidden" value="<?php echo $formid; ?>" /> 
    <p><span class="required">*</span> = required fields.</p> 
    <table border="0" cellspacing="5" cellpadding="5"> 
    <?php 
    $field_labels_col = mysql_result($query, 0, "field_label"); 
    $field_labels = array(); 
    $field_labels =(explode('|', $field_labels_col)); 

    $field_types_col = mysql_result($query, 0, "field_type"); 
    $field_types = array(); 
    $field_types =(explode('|', $field_types_col)); 

    $field_option1_col = mysql_result($query, 0, "field_option1"); 
    $field_option1 = array(); 
    $field_option1 =(explode('|', $field_option1_col)); 

    $field_option2_col = mysql_result($query, 0, "field_option2"); 
    $field_option2 = array(); 
    $field_option2 =(explode('|', $field_option2_col)); 

    $field_validtype_col = mysql_result($query, 0, "field_validtype"); 
    $field_validtype = array(); 
    $field_validtype =(explode('|', $field_validtype_col)); 

    $i = 0; 
    while (array_key_exists($i, $field_labels)) 
    { 
    if(!isset($title)) { $title = ''; } 
    if($title != $field_labels[$i]) { 

    if(isset($field_labels[$i])) { $title = $field_labels[$i]; } 
    if(isset($title)) { $stripped = urlify($title); } 
    if(isset($field_types[$i])) { $type = $field_types[$i]; } 
    if(isset($field_option1[$i])) { $option1 = $field_option1[$i]; } 
    if(isset($field_option2[$i])) { $option2 = $field_option2[$i]; } 
    if(isset($field_validtype[$i])) { $valid = $field_validtype[$i]; } 

    echo '<tr>'; 
     echo '<td align="left" valign="top">'.$title.'</td>'; 
     echo '<td align="left" valign="top">'; 

     // -- INPUT FIELDS -- 
     if($type == "input") { 
      echo '<input name="'.$stripped.'" type="text" id="'.$stripped.'" size="'.$option1.'" maxlength="'.$option2.'" />'; 
      if($valid != '') { echo '<span class="required">*</span>'; } 
     } 

     // -- TEXT AREAS -- 
     if($type == "textarea") { 
      echo '<textarea name="'.$stripped.'" cols="'.$option1.'" rows="size="'.$option2.'"" id="'.$stripped.'"></textarea>'; 
     } 

     // -- DROP DOWNS -- 
     if($type == "dropdown") { 
      echo '<select name="'.$stripped.'" id="'.$stripped.'">'; 
       $l = 0; 
       while (array_key_exists($l, $field_labels)) 
       { 
       $thisoption = $field_option1[$l]; 
       $thistitle = $field_labels[$l]; 
       if($thistitle == $title) { 
       echo '<option value="'.$thisoption.'">'.$thisoption.'</option>'; 
       } 
       $l++; 
       } 
      echo'</select>'; 
     } 

     // -- RADIO BUTTONS -- 
     if($type == "radiobutton") { 
      $l = 0; 
      while (array_key_exists($l, $field_labels)) 
      { 
      $thisoption = $field_option1[$l]; 
      $thistitle = $field_labels[$l]; 
      $thisstripped = urlify($field_labels[$l]); 
      if($thistitle == $title) { 
       echo '<label><input type="radio" name="'.$thistitle.'" id="'.$thisstripped.'" value="'.$option1.'">'.$thisoption.'</label><br />'; 
      } 
      $l++; 
      } 
     } 

     // -- SIMPLE YES/NO -- 
     if($type == "yesno") { 
      echo '<label><input type="radio" name="'.$stripped.'" id="'.$stripped.'" value="Yes">Yes</label>'; 
      echo '<label><input type="radio" name="'.$stripped.'" id="'.$stripped.'" value="No">No</label>'; 
     } 

     // -- FILE UPLOADS -- 
     if($type == "fileupload") { 
      echo '<input type="file" name="'.$stripped.'" id="'.$stripped.'" size="10"> (2mb limit)'; 
     } 

     echo '</td>'; 
    echo '</tr>'; 
    } 
    $i++; 
    } 
?> 
    <tr id="comment-div"><td colspan="2"><input name="user-comment" size="3" value="" /></td></tr> 

    <tr> 
     <td colspan="2"> 
      <?php if(mysql_result($query, 0, "submit_text") != "") { ?> 
       <p><br /><?php echo mysql_result($query, 0, "submit_text"); ?></p> 
      <?php } ?> 
      <div id="result"><strong>Loading... please wait.</strong></div> 
      <!-- <input type="submit" name="button" id="button" value="<?php echo mysql_result($query, 0, "submit_button"); ?>" /> --> 



     </td> 
    </tr> 
    </table> 
</form> 
+0

將標題移動到代碼的第一行並查看它是如何進行的。它看起來像你的瀏覽器不接收標題。 –

+0

也嘗試使用輸出緩衝來捕獲'imagejpeg()'的數據,然後在腳本結尾處回顯它。 –

+0

我測試過它在我的本地工作正常。 –

回答

1

我猜你發佈的代碼不是整個文件:嘗試刪除<?php?>標籤前後的所有內容。你甚至可以remove the final tag ?>(這是很多人採用的風格習慣,因爲這個問題)。

這會影響您正在執行的每個其他文件:PHP標記之外的每個空格,換行符或其他內容或PHP回顯的其他內容都會添加到圖像內容中,並最終導致圖像損壞。此外,PHP通知和警告也是一個問題。

爲了清楚起見,執行文件格式必須爲:

<?php // nothing before this line 
// no other PHP tags till the end 
... // PHP code with no echo calls 
// no closing tag 

在你的代碼,你這樣做:

header("Content-type: image/png"); 
# ... 
echo "<img src='image/png;base64," . base64_encode($x)."'>"; 

你輸出PNG和HTML,這不可避免地sorrounding它破壞您的圖像內容。您必須刪除HTML(echo $x)並使用來自其他文件f.e的圖像。與

<img src="png_generator_script.php"/> 

或者使用腳本的HTML頁面,刪除

header("Content-type: image/png"); 
+0

是的,我不能發佈我們在我們的cms中使用的代碼,但我的問題不是本地的,如果我運行驗證碼圖像拉動罰款和每件事情的作品,但一旦我將它添加到我們的CMS的插件它告訴我失敗加載圖像的給定URL。加上我刪除了所有的白色間距。我可以給你的文件代碼來檢查? – Code

+0

所以這個問題是在您調用圖像文件腳本時執行的CMS文件。您必須確保之前稱爲圖像生成腳本(庫等)的文件符合我在答案中編寫的格式。這是一個你必須做的檢查,你不能發佈我們所有的文件。這可能是一個長期的工作 – mdesantis

+0

非常感謝我很多人沒有得到它的工作,但生病繼續挖掘,如果我來正確的生病發布它爲你們看到 – Code

0

此代碼工作正常。我已經在Chrome瀏覽器本地進行了測試,它使用的是Chrome瀏覽器的Chrome &。

+0

檢查我的代碼在phpfiddle.org鏈接ID:btf-i1a是鏈接ID,看看你是否可以找出我會出錯的地方 – Code

+0

當我把你的代碼放在http://phpfiddle.org/的編輯器中,並點擊運行按鈕時,它會給出一個圖像作爲輸出,說'簡單的文本字符串'。 –

+0

再試一次我正忙於修改代碼,但它回到原始源代碼 – Code

0

我面臨同樣的問題。刪除所有HTML標記,而不是它會100%的工作..

另存爲PHP文件..的index.php

<?php 
/** 
* PHP GD 
* create a simple image with GD library 
* 
*/ 
//setting the image header in order to proper display the image 
header("Content-Type: image/png"); 
//try to create an image 
$im = @imagecreate(800, 600) 
    or die("Cannot Initialize new GD image stream"); 
//set the background color of the image 
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD); 
//set the color for the text 
$text_color = imagecolorallocate($im, 133, 14, 91); 
//adf the string to the image 
imagestring($im, 5, 300, 300, "I'm a pretty picture:))", $text_color); 
//outputs the image as png 
imagepng($im); 
//frees any memory associated with the image 
imagedestroy($im); 
?> 

...

<html> 
<head> 
</head> 
<body> 

<?php 
/** 
* PHP GD 
* create a simple image with GD library 
* 
*/ 
//setting the image header in order to proper display the image 
header("Content-Type: image/png"); 
//try to create an image 
$im = @imagecreate(800, 600) 
    or die("Cannot Initialize new GD image stream"); 
//set the background color of the image 
$background_color = imagecolorallocate($im, 0xFF, 0xCC, 0xDD); 
//set the color for the text 
$text_color = imagecolorallocate($im, 133, 14, 91); 
//adf the string to the image 
imagestring($im, 5, 300, 300, "I'm a pretty picture:))", $text_color); 
//outputs the image as png 
imagepng($im); 
//frees any memory associated with the image 
imagedestroy($im); 
?> 
</body> 
</html>