[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
http://scr.hu/1yl7/9608b


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
http://scr.hu/1yl7/63kgo


and here is full code:
Code: Select all
   <?php

         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;
      
      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);
      
   ?>


Pastebin version:
Code: Select all
http://pastebin.com/wDvNYwDm


What causing this problem?
B
6
S
1
Posts: 57
Reputation: 514

Return to Open Topic

Who is online

Users browsing this forum: Klausty and 3 guests