1

我已经设置imagealphablending了我创建的每个图像,仍然有黑色背景。可能是什么问题呢?

$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);
    }
4

1 回答 1

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);
}

输出

于 2013-05-01T09:02:41.723 回答