[PHP] Issue With GD library

Chat about anything not covered in these forums, but keep it civil!

Post » Sun Jan 04, 2015 2:08 pm

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

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: Select all

and here is full code:
Code: Select all

         function MakeRandomTxt($lenght = 5){
            $characters = '[email protected]#$%^&*()_+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;
         $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');

Pastebin version:
Code: Select all

What causing this problem?
Posts: 57
Reputation: 514

Return to Open Topic

Who is online

Users browsing this forum: No registered users and 3 guests