Pregunta

I have code that creates an image - it has a background colour of black of which I can't seem to find in the code what does this.

Also, if you can show me a much cleaner/better/more efficient way of writing this code that would be fabulous as I just know it isn't.

The basic functionality of the code is to create an image, with random numbers on it to be used as a captcha type image. So keeping that in mind, can anyone help?

Also, as you might be able to tell, I have code that adds lines to the image so as to attempt to confuse the spam bot in case it tries to read the image. If you could help me please to write code that does the same but is much cleaner, I thank you so much!

<?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);
?>
¿Fue útil?

Solución

I think it's this

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

at least when I tried changing 44, 44, 44 -> 255, 255, 255 it turns white

not sure what you can do to clean up the code more, but sometimes I would do this when the function get too stupidly long

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

some commenting would help too, even if it's just saying what this block of code does

eg: //create dotted lines for distraction

or: //print the image

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top