是什么让我的代码中的图像背景变黑?

时间:2014-01-31 18:12:45

标签: php image writer

我有创建图像的代码 - 它的背景颜色为黑色,我在代码中似乎无法找到它的作用。

另外,如果你能告诉我一个更清洁/更好/更有效的编写代码的方法,那将是非常棒的,因为我知道它不是。

代码的基本功能是创建一个图像,其上有随机数,可用作验证码类型图像。所以记住这一点,任何人都可以帮忙吗?

此外,正如您可能能够说的那样,我有代码可以为图像添加线条,以便在尝试读取图像时尝试混淆垃圾邮件机器人。如果你能帮助我,请编写同样但更清洁的代码,我非常感谢你!

<?php
session_start();
$_SESSION['replace'] = rand(1,99999);   
$image = imagecreatetruecolor(80, 20); 

for ($i=0; $i < rand(20,40); $i++) {
$x = rand(0, 70);
$y = rand(0, 20);
imagedashedline($image, $x, $y, $x+rand(0,10), $y+rand(0,240), imagecolorallocate($image,    rand(0,1),rand(0,1),rand(1,2)));
imagedashedline($image, $x, $y, $x+rand(0,11), $y+rand(0,34), imagecolorallocate($image, 255,rand(50,240),rand(241,240)));
}

$pos_x = rand(4,15);
$strArr = str_split($_SESSION['replace']);
foreach ($strArr as $str){
    $font_size = 5;
    imagestring($image,$font_size, $pos_x, $f, $str, imagecolorallocate($image, $s,$x,$s));
    imagestring($image,$font_size, $pos_x, $f, $str, imagecolorallocate($image, 255,rand(50,240),rand(241,240)));
    $pos_x = $pos_x + rand(9,11); // letter spacing 
}

$color = imagecolorallocate($image, 44, 44, 44);
imagefill($image, 0, 0, $color);
imageinterlace($image);
header("Content-type: image/gif");
imagegif($image);
imagedestroy($image);
?>

1 个答案:

答案 0 :(得分:0)

我认为就是这个

$color = imagecolorallocate($image, 44, 44, 44);

至少当我尝试改变44,44,44 - &gt; 255,255,255变为白色

不确定你可以做些什么来清理代码,但有时我会在函数变得过于愚蠢时执行此操作

imagestring($image,$font_size, $pos_x, $f,
            $str, imagecolorallocate($image, $s,$x,$s)
            );

一些评论也会有所帮助,即使它只是说这段代码做了什么

例如://创建虚线以分散注意力

或://打印图像