//将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成
  $im = imagecreate(60,20)  
   $strx=rand(3,8);
    for($i=0;$i<4;$i++){
    $strpos=rand(1,6);
    imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);
    $strx+=rand(8,12);
    }  上面的代码的$strx=rand(3,8);$strx+=rand(8,12);这两句不是很明白,这两句怎么将$im的60平分呢,还是这里根本不是平分,可以帮我解释一下这两句代码的意思吗?特别是$strx+=rand(8,12);这句,先谢谢了

解决方案 »

  1.   

    完整可运行的程序是这样:<?php
    $im = imagecreate(60,20);
    $text = "hell"; // 验证码字符,四字节
    $bg = imagecolorallocate($im, 255, 255, 255);
    $textcolor = imagecolorallocate($im, 0, 0, 255);$strx=rand(3,8);
    for($i=0;$i<4;$i++){
      $strpos=rand(1,6); // 这个是纵坐标y
      imagestring($im,5,$strx,$strpos, substr($text,$i,1), $textcolor);
      $strx+=rand(8,12);
    }header("Content-type: image/png");
    imagepng($im);$strx是横坐标,$strx+=rand(8,12); 得到的结果总是越来越大,这样每个字符间都会保持距离(距离多少随机,但一定会有 8>5)
      

  2.   

    假设每次得到总是最大的随机数, $strx的值会这么变化 : 8, 20, 32, 44
    横坐标最大也只能到44像素,加上字体大小5像素.(还要考虑非等宽字体,例如 l, w。但60应该足够)
      

  3.   

    $strx += rand(8,12); 是 $strx =  $strx + rand(8,12);  的简写
      

  4.   

    知道是$strx = $strx + rand(8,12)的简写,不过我也得不到那个20,32,44.
      

  5.   

    精神可嘉,但缺乏灵活性$strx = 8;//rand(3,8) 介于 3 到 8 之间,取最大值 8
    for($i=0; $i<4; $i++) {
      echo $strx += 12;//rand(8,12) 介于 8 到 12 之间,取最大值 12
      echo '<br>';
    }
    20
    32
    44
    56