slpctrl Posted September 6, 2008 Share Posted September 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/ Share on other sites More sharing options...
ankhmor Posted September 6, 2008 Share Posted September 6, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/#findComment-635355 Share on other sites More sharing options...
.josh Posted September 6, 2008 Share Posted September 6, 2008 You can take all those rand(x,y) numbers and stick it into an array (a multi-dim array would probably be better) and then loop through the array. Quote Link to comment https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/#findComment-635363 Share on other sites More sharing options...
slpctrl Posted September 6, 2008 Author Share Posted September 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/#findComment-635429 Share on other sites More sharing options...
.josh Posted September 6, 2008 Share Posted September 6, 2008 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 What have you tried? Have you looked at what the gd library has to offer? Quote Link to comment https://forums.phpfreaks.com/topic/123043-optimizing-with-loops/#findComment-635458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.