Jump to content

Optimizing with loops


slpctrl

Recommended Posts

Here is my captcha again, I wanted to optimize the redundant 'line' lines with a loop. How would I go about doing that though and still maintain the randomness of the lines? Here is my code to create the CAPTCHA image:

 

<?php
session_start();
$str1 = str_shuffle(md5(microtime() * mktime()));
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$color = imagecolorallocate($captcha,rand(0,50),rand(0,50),rand(0,50));
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(50,200),rand(0,100),rand(50,200),rand(80,200),$color);
imageline($captcha,rand(50,250),rand(0,100),rand(0,200),rand(50,200),$color);
imageline($captcha,rand(70,150),rand(0,100),rand(50,200),rand(0,200),$color);
imageline($captcha,rand(20,150),rand(0,100),rand(20,150),rand(35,200),$color);
imageline($captcha,rand(80,200),rand(0,100),rand(50,200),rand(85,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imagestring($captcha,rand(1,5),rand(0,100),rand(0,50),$str,$color);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

 

How do I remoev all the imageline()s with a loop, while still maintaining the random values?

Link to comment
https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/
Share on other sites

Well I dont think you can loop all of them but im pretty sure u can at least loop those ones

 

imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);

imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);

imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);

imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);

imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);

How might that look? I've tried different ones, none seemed to work :(.

 

Unrelated here, how can I do different things with the text here in the code? I'm wanting to maybe make the text semi transparent, bold/bigger and red because I'm gonna add various other texts to the background to further secure the CAPTCHA. Anyone know how I can do this? Here is the code to create the image:

 

<?php
session_start();
$str1 = str_shuffle(md5(microtime() * mktime()));
$str = substr($str1,0,5);
$captcha = imagecreatefrompng("./captcha.png");
$color = imagecolorallocate($captcha,rand(0,50),rand(0,50),rand(0,50));
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$black);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imageline($captcha,rand(50,200),rand(0,100),rand(50,200),rand(80,200),$color);
imageline($captcha,rand(50,250),rand(0,100),rand(0,200),rand(50,200),$color);
imageline($captcha,rand(70,150),rand(0,100),rand(50,200),rand(0,200),$color);
imageline($captcha,rand(20,150),rand(0,100),rand(20,150),rand(35,200),$color);
imageline($captcha,rand(80,200),rand(0,100),rand(50,200),rand(85,200),$color);
imageline($captcha,rand(0,50),rand(0,100),rand(0,200),rand(0,200),$color);
imagestring($captcha,rand(1,5),rand(0,100),rand(0,50),$str,$color);
$_SESSION['str'] = md5($str);
header("Content-type: image/png");
imagepng($captcha);
?>

 

Can't figure out how to make the text semi transparent or bold or bigger etc though :(:P

How might that look? I've tried different ones, none seemed to work :(

 

How might what look? An array? A loop? 

 

Can't figure out how to make the text semi transparent or bold or bigger etc though :(:P

 

What have you tried?  Have you looked at what the gd library has to offer?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.