2013-05-01 35 views
1

我在每個創建的圖像上都設置了imagealphablending,仍然存在黑色背景。可能是什麼問題呢?PHP驗證碼生成器不會生成透明的PNG。爲什麼?

$systemSettings = array(
    'captcha' => array(
     'colors' => array(array(30, 80, 180), array(20, 160, 40), array(220, 40, 10)), 
     'fonts' => array('AntykwaBold.ttf', 'Candice.ttf', 'Carbon.ttf', 'Duality.ttf', 'Heineken.ttf', 'Jura.ttf', 'StayPuft.ttf', 'TimesNewRomanBold.ttf', 'VeraSansBold.ttf'), 
     'size' => array('width' => '200', 'height' => '50') 
    ) 
); 

function generateCaptcha() { 
     global $systemSettings; 

     $randomLetters = "abcdef"; 
     $scale = 3; 
     $width = $systemSettings['captcha']['size']['width']; 
     $height = $systemSettings['captcha']['size']['height']; 

     $im = imagecreatetruecolor($width * $scale, $height * $scale); 
     imagealphablending($im, true); 

     //$GdBgColor = imagecolorallocate($im, 250, 250, 250); 
     //imagefilledrectangle($im, 0, 0, $width * $scale, $height * $scale, $GdBgColor); 

     $color = $systemSettings['captcha']['colors'][mt_rand(0, sizeof($systemSettings['captcha']['colors']) - 1)]; 
     $GdFgColor = imagecolorallocate($im, $color[0], $color[1], $color[2]); 

     $fontfile = 'visual/fonts/' . $systemSettings['captcha']['fonts'][array_rand($systemSettings['captcha']['fonts'])]; 
     $x = 20 * $scale; 
     $y = round(($height * 0.75) * $scale); 
     $length = strlen($randomLetters); 

     for ($i = 0; $i < $length; $i++) { 
      $degree = rand(-10, 10); 
      $fontsize = rand(20, 25) * $scale; 
      $letter = substr($randomLetters, $i, 1); 
      $coords = imagettftext($im, $fontsize, $degree, $x, $y, $GdFgColor, $fontfile, $letter); 
      $x += ($coords[2] - $x); 
     } 

     $k = rand(0, 100); 
     $xp = $scale * 12 * rand(1, 3); 
     for ($i = 0; $i < ($width * $scale); $i++) 
      imagecopy($im, $im, $i - 1, sin($k + $i/$xp) * ($scale * 5), $i, 0, 1, $height * $scale); 
     $k = rand(0, 100); 
     $yp = $scale * 12 * rand(1, 3); 
     for ($i = 0; $i < ($height * $scale); $i++) 
      imagecopy($im, $im, sin($k + $i/$yp) * ($scale * 14), $i - 1, 0, $i, $width * $scale, 1); 

     imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR); 

     $imResampled = imagecreatetruecolor($width, $height); 
     imagecopyresampled($imResampled, $im, 0, 0, 0, 0, $width, $height, $width * $scale, $height * $scale); 

     imagealphablending($imResampled, true); 

     header("Content-type: image/png"); 
     imagepng($imResampled); 
     imagedestroy($im); 
     imagedestroy($imResampled); 
    } 
+1

[http://stackoverflow.com/questions/8437665/how-to-create-an-image-with-transparent-background]( http://stackoverflow.com/questions/8437665/how-to-create-an-image-with-transparent-background)或[http://stackoverflow.com/questions/6109832/php-gd-createa-a-透明PNG圖像](http://stackoverflow.com/questions/6109832/php-gd-create-a-transparent-png-image)。總是使用谷歌搜索你的問題。也stackoverflow.com是一個大網站,你可能會找到解決方案,您的問題,首先搜索它。 – machineaddict 2013-05-01 08:48:21

+0

看到我的答案,我評論了我在代碼中添加的透明代碼 – 2013-05-01 09:13:07

+0

嗯,這個驗證碼很容易破解... – 2013-05-01 14:07:55

回答

0

嘗試這種

$systemSettings = array(
    'captcha' => array(
    'colors' => array(array(30, 80, 180), array(20, 160, 40), array(220, 40, 10)), 
    'fonts' => array('AntykwaBold.ttf', 'Candice.ttf', 'Carbon.ttf', 'Duality.ttf', 'Heineken.ttf', 'Jura.ttf', 'StayPuft.ttf', 'TimesNewRomanBold.ttf', 'VeraSansBold.ttf'), 
    'size' => array('width' => '200', 'height' => '50') 
) 
); 

function generateCaptcha() { 
    global $systemSettings; 

    $randomLetters = "abcdef"; 
    $scale = 3; 
    $width = $systemSettings['captcha']['size']['width']; 
    $height = $systemSettings['captcha']['size']['height']; 

    $im = imagecreatetruecolor($width * $scale, $height * $scale); 
    // enable alpha blending on the destination image. 
    imagealphablending($im, true); 
    // Allocate a transparent color and fill the new image with it. 
    // Without this the image will have a black background instead of being transparent. 
    $transparent = imagecolorallocatealpha($im, 0, 0, 0, 127); 
    imagefill($im, 0, 0, $transparent); 
    // disable alpha blending on the destination image. 
    imagealphablending($im, false); 

    // save the alpha 
    imagesavealpha($im,true); 

    //$GdBgColor = imagecolorallocate($im, 250, 250, 250); 
    //imagefilledrectangle($im, 0, 0, $width * $scale, $height * $scale, $GdBgColor); 

    $color = $systemSettings['captcha']['colors'][mt_rand(0, sizeof($systemSettings['captcha']['colors']) - 1)]; 
    $GdFgColor = imagecolorallocate($im, $color[0], $color[1], $color[2]); 

    $fontfile = 'visual/fonts/' . $systemSettings['captcha']['fonts'][array_rand($systemSettings['captcha']['fonts'])]; 
    $x = 20 * $scale; 
    $y = round(($height * 0.75) * $scale); 
    $length = strlen($randomLetters); 

    for ($i = 0; $i < $length; $i++) { 
     $degree = rand(-10, 10); 
     $fontsize = rand(20, 25) * $scale; 
     $letter = substr($randomLetters, $i, 1); 
     $coords = imagettftext($im, $fontsize, $degree, $x, $y, $GdFgColor, $fontfile, $letter); 
     $x += ($coords[2] - $x); 
    } 

    $k = rand(0, 100); 
    $xp = $scale * 12 * rand(1, 3); 
    for ($i = 0; $i < ($width * $scale); $i++) 
     imagecopy($im, $im, $i - 1, sin($k + $i/$xp) * ($scale * 5), $i, 0, 1, $height * $scale); 
    $k = rand(0, 100); 
    $yp = $scale * 12 * rand(1, 3); 
    for ($i = 0; $i < ($height * $scale); $i++) 
     imagecopy($im, $im, sin($k + $i/$yp) * ($scale * 14), $i - 1, 0, $i, $width * $scale, 1); 

    imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR); 

    $imResampled = imagecreatetruecolor($width, $height); 
    // enable alpha blending on the destination image. 
    imagealphablending($imResampled, true); 
    // Allocate a transparent color and fill the new image with it. 
    // Without this the image will have a black background instead of being transparent. 
    $transparent = imagecolorallocatealpha($imResampled, 0, 0, 0, 127); 
    imagefill($imResampled, 0, 0, $transparent); 
    // disable alpha blending on the destination image. 
    imagealphablending($imResampled, false); 

    // save the alpha 
    imagesavealpha($imResampled,true); 
    imagecopyresampled($imResampled, $im, 0, 0, 0, 0, $width, $height, $width * $scale, $height * $scale); 
    header("Content-type: image/png"); 
    imagepng($imResampled); 
    imagedestroy($im); 
    imagedestroy($imResampled); 
} 

output