2011-05-28 70 views
3

我想知道爲什麼由此腳本生成的圖像中的字符不匹配爲會話設置的字符。我找不到這是爲什麼,如果有人能夠幫助,那會很好。謝謝,Lea。會話與圖像不匹配PHP

// Font directory + font name 
$font = 'wordpress/wp-content/themes/boldy/fonts/SF Juggernaut Bold.ttf'; 
// Total number of lines 
$lineCount = 20; 
// Size of the font 
$fontSize = 19; 
// Height of the image 
$height = 38; 
// Width of the image 
$width = 120; 
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image"); 
// Set the Background Color RGB 
$backColor = imagecolorallocate($img_handle, 242, 242, 242); 
// Set the Line Color RGB 
$lineColor = imagecolorallocate($img_handle, 207, 207, 207); 
// Set the Text Color RGB 
$txtColor = imagecolorallocate($img_handle, 95, 95, 95); 

// Generate 
$string = "bcdefghijklmopqrstwxyz023456789"; 
for($i=0;$i<6;$i++){ 
    $pos = rand(0,36); 
    $str .= $string{$pos}; 
} 

$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function'); 

$x = ($width - $textbox[4])/2; 
$y = ($height - $textbox[5])/2; 

imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function'); 

for($i=0;$i<$lineCount;$i++){ 
    $x1 = rand(0,$width);$x2 = rand(0,$width); 
    $y1 = rand(0,$width);$y2 = rand(0,$width); 
    imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor); 
} 

header('Content-Type: image/jpeg'); 
imagejpeg($img_handle,NULL,100); 
imagedestroy($img_handle); 

session_start(); 
$_SESSION['img_number'] = $str; 

UPDATE:在測試中,我注意到,第一次加載圖像,會話沒有得到設定,從而使圖像被刷新時,會話設置與最後$ STR值

+0

嘗試把你的會話開始/會話變量設置代碼的'標題(「內容類型:圖像/ JPEG」)之前;' – 2011-05-28 18:34:44

+0

@onteria_ - 這似乎不有所作爲。會話字符串似乎與圖像字符不同步。 – Lea 2011-05-28 18:52:17

回答

1

如果您的頭文件已經發送,您可能會遇到調用session_start()的問題。

變化

header('Content-Type: image/jpeg'); 
imagejpeg($img_handle,NULL,100); 
imagedestroy($img_handle); 

session_start(); 
$_SESSION['img_number'] = $str; 

session_start(); 
header('Content-Type: image/jpeg'); 
imagejpeg($img_handle,NULL,100); 
imagedestroy($img_handle); 

$_SESSION['img_number'] = $str; 
+0

不,它在那裏:'$ str。= $ string {$ pos};' – 2011-05-28 18:35:05

+0

謝謝。沒有尋找這樣的線......忘記PHP允許這樣的聲明。 – erisco 2011-05-28 18:37:10

0

如果您調試腳本的東西,如螢火蟲或devtools,然後調用圖像的兩倍。

嘗試禁用您的firebug/devtools,您將看到PROFIT。

+0

我希望這對我來說是真實的,但我已經禁用了螢火蟲,它仍然是一樣的。 – Lea 2011-05-28 18:53:07

+0

也嘗試添加一些登錄,以瞭解您的驗證碼腳本調用的次數和位置。 – gaRex 2011-05-28 18:59:18

0
session_start(); 

// Font directory + font name 
$font = 'wordpress/wp-content/themes/boldy/fonts/SF Juggernaut Bold.ttf'; 
// Total number of lines 
$lineCount = 20; 
// Size of the font 
$fontSize = 19; 
// Height of the image 
$height = 38; 
// Width of the image 
$width = 120; 
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image"); 
// Set the Background Color RGB 
$backColor = imagecolorallocate($img_handle, 242, 242, 242); 
// Set the Line Color RGB 
$lineColor = imagecolorallocate($img_handle, 207, 207, 207); 
// Set the Text Color RGB 
$txtColor = imagecolorallocate($img_handle, 95, 95, 95); 

// Generate 
$string = "bcdefghijklmopqrstwxyz023456789"; 
$str = ''; 
for($i=0;$i<6;$i++){ 
    $pos = rand(0,36); 
    $str .= substr($string, $pos, 1); 
} 

$_SESSION['img_number'] = $str; 

$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function'); 

$x = ($width - $textbox[4])/2; 
$y = ($height - $textbox[5])/2; 

imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function'); 

for($i=0;$i<$lineCount;$i++){ 
    $x1 = rand(0,$width);$x2 = rand(0,$width); 
    $y1 = rand(0,$width);$y2 = rand(0,$width); 
    imageline($img_handle,$x1,$y1,$x2,$y2,$lineColor); 
} 

header('Content-Type: image/jpeg'); 
imagejpeg($img_handle,NULL,100); 
imagedestroy($img_handle); 
+0

這似乎沒有任何區別。謝謝 – Lea 2011-05-28 21:09:43

+0

Lea,你有沒有得到任何字體輸出? – 2011-06-04 21:23:14