Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP验证码生成器不生成透明PNG。为什么?_Php_Image_Gd_Captcha_Alpha - Fatal编程技术网

PHP验证码生成器不生成透明PNG。为什么?

PHP验证码生成器不生成透明PNG。为什么?,php,image,gd,captcha,alpha,Php,Image,Gd,Captcha,Alpha,我已经在我创建的每个图像上设置了imagealphablending,但仍然有一个黑色背景。有什么问题吗 $systemSettings = array( 'captcha' => array( 'colors' => array(array(30, 80, 180), array(20, 160, 40), array(220, 40, 10)), 'fonts' => array('AntykwaBold.ttf', 'Candice

我已经在我创建的每个图像上设置了
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);
    }
$systemSettings=array(
“验证码”=>数组(
'colors'=>数组(数组(30,80,180)、数组(20,160,40)、数组(220,40,10)),
'fonts'=>数组('AntykwaBold.ttf','Candice.ttf','Carbon.ttf','Duality.ttf','Heineken.ttf','Jura.ttf','StayPuft.ttf','TimesNewRomanBold.ttf','VeraSansBold.ttf'),
'size'=>array('width'=>'200','height'=>'50')
)
);
函数generateCaptcha(){
全球系统设置;
$randomLetters=“abcdef”;
$scale=3;
$width=$systemSettings['captcha']['size']['width'];
$height=$systemSettings['captcha']['size']['height'];
$im=ImageCreateTureColor($width*$scale,$height*$scale);
imagealphabling($im,true);
//$GdBgColor=imagecolorallocate($im,250250250);
//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=圆形($height*0.75)*$scale);
$length=strlen(随机字母);
对于($i=0;$i<$length;$i++){
$degree=兰特(-10,10);
$fontsize=兰特(20,25)*$scale;
$letter=substr($randomLetters,$i,1);
$coords=imagettftext($im、$fontsize、$degree、$x、$y、$GdFgColor、$fontfile、$letter);
$x+=($coords[2]-$x);
}
$k=兰特(0,100);
$xp=$scale*12*rand(1,3);
对于($i=0;$i<($width*$scale);$i++)
图像复制($im,$i-1,sin($k+$i/$xp)*($scale*5),$i,0,1,$height*$scale);
$k=兰特(0,100);
$yp=$scale*12*rand(1,3);
对于($i=0;$i<($height*$scale);$i++)
图像复制($im、$im、sin($k+$i/$yp)*($scale*14),$i-1,0,$i,$width*$scale,1);
图像过滤器($im、IMG\u过滤器\u高斯\u模糊);
$imResampled=imagecreatetruecolor($width,$height);
imagecopyresampled($imResampled,$im,0,0,0,$width,$height,$width*$scale,$height*$scale);
imagealphablending($imResampled,true);
标题(“内容类型:图像/png”);
imagepng($imResampled);
图像处理(港币);;
imagedestroy(重新采样);
}
试试这个

 $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);
}
$systemSettings=array(
“验证码”=>数组(
'colors'=>数组(数组(30,80,180)、数组(20,160,40)、数组(220,40,10)),
'fonts'=>数组('AntykwaBold.ttf','Candice.ttf','Carbon.ttf','Duality.ttf','Heineken.ttf','Jura.ttf','StayPuft.ttf','TimesNewRomanBold.ttf','VeraSansBold.ttf'),
'size'=>array('width'=>'200','height'=>'50')
)
);
函数generateCaptcha(){
全球系统设置;
$randomLetters=“abcdef”;
$scale=3;
$width=$systemSettings['captcha']['size']['width'];
$height=$systemSettings['captcha']['size']['height'];
$im=ImageCreateTureColor($width*$scale,$height*$scale);
//在目标图像上启用alpha混合。
imagealphabling($im,true);
//分配一个透明颜色并用它填充新图像。
//如果没有这一点,图像将具有黑色背景,而不是透明的。
$transparent=imagecolorallocatealpha($im,0,0,0,127);
imagefill($im,0,0,$transparent);
//在目标图像上禁用alpha混合。
ImageAlphabling($im,false);
//拯救阿尔法
imagesavealpha($im,true);
//$GdBgColor=imagecolorallocate($im,250250250);
//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=圆形($height*0.75)*$scale);
$length=strlen(随机字母);
对于($i=0;$i<$length;$i++){
$degree=兰特(-10,10);
$fontsize=兰特(20,25)*$scale;
$letter=substr($randomLetters,$i,1);
$coords=imagettftext($im、$fontsize、$degree、$x、$y、$GdFgColor、$fontfile、$letter);
$x+=($coords[2]-$x);
}
$k=兰特(0,100);
$xp=$scale*12*rand(1,3);
对于($i=0;$i<($width*$scale);$i++)
图像复制($im,$i-1,sin($k+$i/$xp)*($scale*5),$i,0,1,$height*$scale);
$k=兰特(0,100);
$yp=$scale*12*rand(1,3);
对于($i=0;$i<($height*$scale);$i++)
图像复制($im、$im、sin($k+$i/$yp)*($scale*14),$i-1,0,$i,$width*$scale,1);
图像过滤器($im、IMG\u过滤器\u高斯\u模糊);
$imResampled=imagecreatetruecolor($width,$height);
//在目标图像上启用alpha混合。
imagealphablending($imResampled,true);
//分配一个透明颜色并用它填充新图像。
//如果没有这一点,图像将具有黑色背景,而不是透明的。
$transparent=imagecolorallocatealpha($imResampled,0,0,0127);
imagefill($imResampled,0,0,$transparent);
//在目标图像上禁用alpha混合。
imagealphablending($imResampled,false);
//拯救阿尔法
imagesavealpha($imResampled,true);
imagecopyresampled($imResampled,$im,0,0,0,$width,$height,$width*$scale,$height*$scale);
标题(“内容类型:图像/png”);
图像PNG($im)