0 Favourites

[PHP] Issue With GD library

  • Some time ago I wrote simple code in PHP to generate an image (captcha) and it works:

    http://scr.hu/1yl7/9608b[/code:5pnrwpy8]
    
    Today I again run code to make some documentation to improve it in future, but there is something weird...
    Output image looks like this:
    [code:5pnrwpy8]http://scr.hu/1yl7/63kgo[/code:5pnrwpy8]
    
    and here is full code:
    [code:5pnrwpy8]
    	<?php
    
    			function MakeRandomTxt($lenght = 5){
    				$characters = '!@#$%^&*()_+0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    				$charactersLength = strlen($characters);
    				$randomString = '';
    					for ($i = 0; $i < $lenght; $i++) {
    						$randomString .= $characters[rand(0, $charactersLength - 1)];
    					}
    				return $randomString;
    			}
    
    			
    		$rnd = MakeRandomTxt();	
    			
    		$string = $rnd;
    		$font_size = rand(24, 48); 
    		$width  = 140+imagefontwidth($font_size)*strlen($string); 
    		$height = 36+imagefontheight($font_size); 
    		$img 	= imagecreate($width,$height); 
    		$bg   	= imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 
    		$color 	= imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); 
    		$len 	= strlen($string); 
    		$ypos 	= 14; 
    		
    		for($i=0;$i<$len;$i++){ 
    		
    			$xpos = 64+$i * imagefontwidth($font_size); 
    			imagechar($img, $font_size+20, $xpos, rand(2, $ypos), $string, $color); 
    			$string = substr($string, 1);    
    			
    		} 
    		header("Content-Type: image/png");
    		imagepng($img, './cp/'.$rnd.'.png'); 
    		imagedestroy($img); 
    		
    	?>
    [/code:5pnrwpy8]
    
    Pastebin version:
    [code:5pnrwpy8]http://pastebin.com/wDvNYwDm[/code:5pnrwpy8]
    
    What causing this problem?
  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
300 118,426
blackhornet's avatar
blackhornet
Unread hot topic
0 Favourites
Spam issue
201 16,836
Tom's avatar
Tom Construct Team Founder
Unread hot topic
157 12,693
megatronx's avatar
megatronx