php生成 曲线图 程序

 更新时间:2016年11月25日 16:58  点击:1623

php生成  曲线图 程序

<?php
/*******************用法*************************
    $gg=new build_graph();
   
    $d1=array(0,62,25,20,20,100,99);  //曲线一
//$d1=array('15'=>5,'16'=>8,'17'=>7,'18'=>9,'19'=>10,'20'=>15,'21'=>9); 改成这个形式啦
    $d2=array(0,80,75,65,100,56,79);  //曲线二
    $d3=array(0,60,50,25,12,56,45);   //曲线三 一下可以继续以此类推
   
    $gg->add_data($d1);
    $gg->add_data($d2);
    $gg->add_data($d3);
   
    $gg->set_colors("ee00ff,dd8800,00ff00"); //对应曲线的颜色
   
    //生成曲线图
    $gg->build("line",0);          //参数0表示显示所有曲线,1为显示第一条,依次类推
   
    //生成矩形图
    //$gg->build("rectangle","2");    //参数0表示显示第一个矩形,1也为显示第一条,其余依次类推
   
///////////////////////////////////////////////////////////
    //自定义图形显示,可任意图形叠加显示
       header("Content-type: image/png");
       $gg->create_cloths();          //画布
       $gg->create_frame();          //画个框先
       //$gg->build_rectangle(2);       //画矩形
       $gg->create_line();             //画线
       $gg->build_line(0);             //画曲线
       imagepng($gg->image);
       imagedestroy($gg->image);
*/
class build_graph {
    var $graphwidth=300;
    var $graphheight=300;
    var $width_num=0;                //宽分多少等分
    var $height_num=10;                //高分多少等分,默认为10
    var $height_var=0;                //高度增量(用户数据平均数)
    var $width_var=0;                //宽度增量(用户数据平均数)
    var $height_max=0;                //最大数据值
    var $array_data=array();          //用户待分析的数据的二维数组
    var $array_error=array();          //收集错误信息
    var $colorBg=array(255,255,255);    //图形背景-白色
    var $colorGrey=array(192,192,192);    //灰色画框
    var $colorBlue=array(0,0,255);       //蓝色
    var $colorRed=array(255,0,0);       //红色(点)
    var $colorDarkBlue=array(0,0,255);    //深色
    var $colorLightBlue=array(200,200,255);       //浅色
    var $array_color;                //曲线着色(存储十六进制数)
    var $image;                      //我们的图像
    //方法:接受用户数据
    function add_data($array_user_data)
    {
       if(!is_array($array_user_data) or empty($array_user_data))
       {
          $this->array_error['add_data']="没有可供分析的数据";
          return false;
          exit();
       }
      
       $i=count($this->array_data);
      
       $this->array_data[$i]=$array_user_data;
      
    }
    //方法:定义画布宽和长
    function set_img($img_width,$img_height){
       $this->graphwidth=$img_width;
       $this->graphheight=$img_height;
    }
    //设定Y轴的增量等分,默认为10份
    function set_height_num($var_y){
       $this->height_num=$var_y;
    }
    //定义各图形各部分色彩
    function get_RGB($color){             //得到十进制色彩
    $R=($color>>16) & 0xff;
    $G=($color>>8) & 0xff;
    $B=($color) & 0xff;
    return (array($R,$G,$B));
    }
    //---------------------------------------------------------------
    #定义背景色
    function set_color_bg($c1,$c2,$c3){
       $this->colorBg=array($c1,$c2,$c3);
    }
    #定义画框色
    function set_color_Grey($c1,$c2,$c3){
       $this->colorGrey=array($c1,$c2,$c3);
    }
    #定义蓝色
    function set_color_Blue($c1,$c2,$c3){
       $this->colorBlue=array($c1,$c2,$c3);
    }
    #定义色Red
    function set_color_Red($c1,$c2,$c3){
       $this->colorRed=array($c1,$c2,$c3);
    }
    #定义深色
    function set_color_DarkBlue($c1,$c2,$c3){
       $this->colorDarkBlue=array($c1,$c2,$c3);
    }
    #定义浅色
    function set_color_LightBlue($c1,$c2,$c3){
       $this->colorLightBlue=array($c1,$c2,$c3);
    }
    //---------------------------------------------------------------
    //方法:由用户数据将画布分成若干等份宽
    //并计算出每份多少像素
    function get_width_num(){
       $this->width_num=count($this->array_data[0]);
    }
   
    function get_max_height(){
       //获得用户数据的最大值
       $tmpvar=array();
         
       foreach($this->array_data as $tmp_value)
       {
          $tmpvar[]=max($tmp_value);
       }
      
       $this->height_max=max($tmpvar);
      
       return max($tmpvar);
    }
   
    function get_height_length(){
       //计算出每格的增量长度(用户数据,而不是图形的像素值)
       $max_var=$this->get_max_height();
       $max_var=round($max_var/$this->height_num);
       $first_num=substr($max_var,0,1);
       if(substr($max_var,1,1)){
          if(substr($max_var,1,1)>=5)
             $first_num+=1;
       }
       for($i=1;$i<strlen($max_var);$i++){
          $first_num.="0";
       }
       return (int)$first_num;
    }
   
    function get_var_wh()  //得到高和宽的增量
    {         
       $this->get_width_num();
       //得到高度增量和宽度增量
       $this->height_var=$this->get_height_length();
      
       $this->width_var=round($this->graphwidth/$this->width_num);
    }
    function set_colors($str_colors){
       //用于多条曲线的不同着色,如$str_colors="ee00ff,dd0000,cccccc"
       $this->array_color=split(",",$str_colors);
    }
######################################################################################################
    function build_line($var_num)
    {
       if(!empty($var_num))
       {                   //如果用户只选择显示一条曲线
          $array_tmp[0]=$this->array_data[$var_num-1];
          $this->array_data=$array_tmp;
       }
      
        //画线
        
       for($j=0;$j<count($this->array_data);$j++)
       {
          list($R,$G,$B)=$this->get_RGB(hexdec($this->array_color[$j]));
         
          $colorBlue=imagecolorallocate($this->image,$R,$G,$B);
         
            $i=0;
              foreach($this->array_data[$j] as $keys=>$values)
            {
                $height_next_pix[]=round($this->array_data[$j][$keys]/$this->height_max*$this->graphheight);
            }
           
            foreach($this->array_data[$j] as $key=>$value)
            {
                $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight);
               
                if($i!=count($this->array_data[$j])-1)
                {
                    imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix[$i+1],$colorBlue);
                }
               
                $i++;
            }
        
        //print_r($height_next_pix);
        // exit;
         /*
          for($i=0;$i<$this->width_num-1;$i++)
          {
             $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
             $height_next_pix=round($this->array_data[$j][$i+1]/$this->height_max*$this->graphheight);
             imageline($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight-$height_next_pix,$colorBlue);
          }*/
       }
      
      
       //画点
      
       $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]);
       for($j=0;$j<count($this->array_data);$j++)
       {
               $i=0;
            foreach($this->array_data[$j] as $key=>$value)
            {
                 $height_pix=round(($this->array_data[$j][$key]/$this->height_max)*$this->graphheight);
                 imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed);
                 imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed);
                 $i++;
            }
       
        /*
          for($i=0;$i<$this->width_num;$i++)
          {
             $height_pix=round(($this->array_data[$j][$i]/$this->height_max)*$this->graphheight);
             imagearc($this->image,$this->width_var*$i,$this->graphheight-$height_pix,6,5,0,360,$colorRed);
             imagefilltoborder($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$colorRed,$colorRed);
          }
         */
       }
      
    }
######################################################################################################
    function build_rectangle($select_gra){
       if(!empty($select_gra)){                   //用户选择显示一个矩形
          $select_gra-=1;
       }
       //画矩形
       //配色
       $colorDarkBlue=imagecolorallocate($this->image, $this->colorDarkBlue[0], $this->colorDarkBlue[1], $this->colorDarkBlue[2]);
       $colorLightBlue=imagecolorallocate($this->image, $this->colorLightBlue[0], $this->colorLightBlue[1], $this->colorLightBlue[2]);
       if(empty($select_gra))
          $select_gra=0;
       for($i=0; $i<$this->width_num; $i++){
          $height_pix=round(($this->array_data[$select_gra][$i]/$this->height_max)*$this->graphheight);
          imagefilledrectangle($this->image,$this->width_var*$i,$this->graphheight-$height_pix,$this->width_var*($i+1),$this->graphheight, $colorDarkBlue);
          imagefilledrectangle($this->image,($i*$this->width_var)+1,($this->graphheight-$height_pix)+1,$this->width_var*($i+1)-5,$this->graphheight-2, $colorLightBlue);
       }
    }
######################################################################################################
    function create_cloths(){
       //创建画布
       $this->image=imagecreate($this->graphwidth+20,$this->graphheight+20);
    }
    function create_frame(){
       //创建画框
       $this->get_var_wh();
       //配色
       $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
       $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
       //创建图像周围的框
       imageline($this->image, 0, 0, 0, $this->graphheight,$colorGrey);
       imageline($this->image, 0, 0, $this->graphwidth, 0,$colorGrey);
       imageline($this->image, ($this->graphwidth-1),0,($this->graphwidth-1),($this->graphheight-1),$colorGrey);
       imageline($this->image, 0,($this->graphheight-1),($this->graphwidth-1),($this->graphheight-1),$colorGrey);
    }
   
    function create_line()
    {
       //创建网格。
       $this->get_var_wh();
       $colorBg=imagecolorallocate($this->image, $this->colorBg[0], $this->colorBg[1], $this->colorBg[2]);
       $colorGrey=imagecolorallocate($this->image, $this->colorGrey[0], $this->colorGrey[1], $this->colorGrey[2]);
       $colorRed=imagecolorallocate($this->image, $this->colorRed[0], $this->colorRed[1], $this->colorRed[2]);
      
       for($i=1;$i<=$this->height_num;$i++)
       {
          //画横线
          $y1=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i);
         
          $y2=($this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i);
         
          imageline($this->image,0,$y1,$this->graphwidth,$y2,$colorGrey);
         
          //标出数字
          imagestring($this->image,2,0,$this->graphheight-($this->height_var/$this->height_max*$this->graphheight)*$i,$this->height_var*$i,$colorRed);
       }
      
       unset($i);
      
       foreach($this->array_data[0] as $key=>$value)
       {
              //画竖线
              imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey);
         
          //标出数字
           imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$key,$colorRed);
          
           $i++;
       }
      
       /*
       for($i=1;$i<=$this->width_num;$i++)
       {
          //画竖线
          imageline($this->image,$this->width_var*$i,0,$this->width_var*$i,$this->graphwidth,$colorGrey);
          //标出数字
          imagestring($this->image,2,$this->width_var*$i,$this->graphheight-15,$i,$colorRed);
       }
       */
    }
    function build($graph,$str_var){
       //$graph是用户指定的图形种类,$str_var是生成哪个数据的图
       header("Content-type: image/jpeg");
       $this->create_cloths();          //先要有画布啊~~
       switch ($graph){
          case "line":
             $this->create_frame();          //画个框先:)
             $this->create_line();          //打上底格线
             $this->build_line($str_var);          //画曲线
             break;
          case "rectangle":
             $this->create_frame();                   //画个框先:)
             $this->build_rectangle($str_var);          //画矩形
             $this->create_line();                   //打上底格线
             break;
       }
       //输出图形并清除内存
       imagepng($this->image);
       imagedestroy($this->image);
    }
}
?>

图片生成缩略图代码

<?php
# Constants
define("IMAGE_BASE", './');
define("MAX_WIDTH", 150);
define("MAX_HEIGHT", 150);

# Get image locationstr_replace('..', '', $_SERVER['QUERY_STRING']);
$image_file = 't.jpg';
$image_path = IMAGE_BASE . "$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
     $img = imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
     $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
     $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

     # Get image size and scale ratio
     $width = imagesx($img);
     $height = imagesy($img);
     $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

     # If the image is larger than the max shrink it
     if ($scale < 1) {
         $new_width =150; //floor($scale*$width);
         $new_height =150;// floor($scale*$height);

         # Create a new temporary image
         $tmp_img = imagecreatetruecolor($new_width, $new_height);

         # Copy and resize old image into new image
         imagecopyresized($tmp_img, $img, 0, 0, 0, 0,$new_width, $new_height, $width, $height);
         imagedestroy($img);
         $img = $tmp_img;
     }
}

# Create error image if necessary
if (!$img) {
     $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
     imagecolorallocate($img,0,0,0);
     $c = imagecolorallocate($img,70,70,70 );
     imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
     imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
imagedestroy($img);
?>


function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
  global $thumbstatus, $imageimpath, $thumbquality;
  if($thumbstatus) {
   list($img_w, $img_h) = $this->attachinfo;
   $targetfile = !$preview ? ($thumbstatus == 1 || $thumbstatus == 3 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
   if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
    if($thumbstatus != 3) {
     $exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$this->targetfile.' '.$targetfile;
     @exec($exec_str, $output, $return);
     if(empty($return) && empty($output)) {
      $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
     }
    } else {
     $imgratio = $img_w / $img_h;
     $thumbratio = $thumbwidth / $thumbheight;
     if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
      $cuty = $img_h;
      $cutx = $cuty * $thumbratio;
     } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
      $cutx = $img_w;
      $cuty = $cutx / $thumbratio;
     }
     $exec_str = $imageimpath.'/convert -crop '.$cutx.'x'.$cuty.'+0+0  '.$this->targetfile.' '.$targetfile;
     @exec($exec_str, $output, $return);
     $exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$targetfile.' '.$targetfile;
     @exec($exec_str, $output, $return);
     if(empty($return) && empty($output)) {
      $this->attach['thumb'] = $thumbstatus == 1 || $thumbstatus == 3 ? 1 : 0;
     }
    }
             }
  }
 }
function Watermark_IM($preview = 0) {
  global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
  $watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
  switch($watermarkstatus) {
   case 1:
    $gravity = 'NorthWest';
    break;
   case 2:
    $gravity = 'North';
    break;
   case 3:
    $gravity = 'NorthEast';
    break;
   case 4:
    $gravity = 'West';
    break;
   case 5:
    $gravity = 'Center';
    break;
   case 6:
    $gravity = 'East';
    break;
   case 7:
    $gravity = 'SouthWest';
    break;
   case 8:
    $gravity = 'South';
    break;
   case 9:
    $gravity = 'SouthEast';
    break;
  }
  $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  if($watermarktype < 2) {
   $watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
   $exec_str = $imageimpath.'/composite'.
    ($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
    ' -quality '.$watermarkquality.
    ' -gravity '.$gravity.
    ' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
  } else {
   $watermarktextcvt = str_replace(array("n", "r", "'"), array('', '', '''), pack("H*", $watermarktext['text']));
   $watermarktext['angle'] = -$watermarktext['angle'];
   $translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
   $skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
   $skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
   $exec_str = $imageimpath.'/convert'.
    ' -quality '.$watermarkquality.
    ' -font "'.$watermarktext['fontpath'].'"'.
    ' -pointsize '.$watermarktext['size'].
    (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
     ' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
     ' -draw "'.
      ' gravity '.$gravity.$translate.$skewX.$skewY.
      ' rotate '.$watermarktext['angle'].
      ' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' ''.$watermarktextcvt.''"' : '').
    ' -fill "rgb('.$watermarktext['color'].')"'.
    ' -draw "'.
     ' gravity '.$gravity.$translate.$skewX.$skewY.
     ' rotate '.$watermarktext['angle'].
     ' text 0,0 ''.$watermarktextcvt.''"'.
    ' '.$this->targetfile.' '.$targetfile;
  }
  @exec($exec_str, $output, $return);
  if(empty($return) && empty($output)) {
   $this->attach['size'] = filesize($this->targetfile);
  }
 }

<?php php 验证码图片 程序
/*
*文件名:class.safeCode.php
*类名:safeCode
*目的:生成web应用时所需的验证码图片
*当前版本:1.0.2
*作者:NoAngels
*联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com
*开发时间:2008年06月20日
*最后更新:2008年06月21日
*更新内容:
*版本1.0.2
*1.更新了对linux主机支持,设置GDFONTPATH环境变量,
* putenv('GDFONTPATH=' . realpath('.'));
*2.对不支持antialias函数的时候避免采用此方法,避免低版本的GD库
*版本1.0.1
*1.更新了绘制指定类型图片,支持类型有:png,jpeg,gif.默认为png(此效果最佳)
*2.优化了部分代码
*/
class safeCode{
        function __construct(){
                #设置默认safe安全保护码
                $this->__safe = substr(md5(time()), 0 , 4);
        }
        function __destruct(){
                #释放资源
                unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName);
        }
        function setCanvas($width = 200, $height = 60){
                #设置画布宽度以及高度.有默认值
                $this->__canvasWidth = $width;
                $this->__canvasHeight = $height;
                $this->__img = imagecreatetruecolor($width, $height);               
                return;
        }
        function setSafe($char){
                #设置安全码,不调用系统会自己设置
                $this->__safe = $char;
                return;
        }
        function setCodeLength($num = 8){
                #设置验证码长度
                $this->__codeLength = $num;
        }       
        function setFont($fontName){
                #设置绘图时所用的字体,字体文件必须与类文件同目录
                $this->__font = $fontName;
                return;
        }
        function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255){
                #设置背景颜色
                $this->__background = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
                return;
        }
        function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128){
                #设置边框颜色
                $this->__borderColor = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
        }
        function setFontColor($arr){
                #设置绘图时所用颜色,参数为颜色集合数组
                foreach($arr as $color){
                        $this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]);
                }
                return;
        }       
        function setSession($sessionName = 'safeCode'){
                #设置session变量名,默认为$_SESSION['safeCode']
                $this->__sessionName = $sessionName;
                #$_SESSION[$sessionName] = $this->__code;
                return;
        }
        function display($type = 'png'){
                #显示图片,可指定显示类型,目前支持png(默认),jpeg,gif
                $this->__setSpacePerChar();
                imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight -2, $this->__background);
                imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor);
                $this->__drawText($this->__colors, $this->__getCode(), $this->__font);
                #修正linux以及低版本的GD库问题
                if(function_exists(imageantialias)){
                   imageantialias($this->__img, true);
                }
                $_SESSION[$this->__sessionName] = $this->__code;
                switch ($type){
                        case 'png':
                                header('Content-type:image/png');                               
                                imagepng($this->__img);       
                        case 'jpeg':
                                header('Content-type:image/jpeg');                               
                                imagejpeg($this->__img, '' , 75);
                        case 'gif':
                                header('Content-type:image/gif');                               
                                imagegif($this->__img);       
                        default:
                                header('Content-type:image/png');                               
                                imagepng($this->__img);                                                                                                               
                }
                imagegif($this->__img);       
        }       
        private function __setSpacePerChar(){
                #私有函数,设置字符间隔
                $this->__spacePerChar = $this->__canvasWidth / $this->__codeLength;
                return;
        }
        private function __getCode(){
                #获取验证码
                $this->__code = substr(md5(time().$this->__safe), rand(0, 24), $this->__codeLength);
                return $this->__code;
        }
        private function  __drawText($colors, $code, $fontName){
                #开始绘图
                #设置GBFONTPATH为当前目录,不然linux环境下会报错
                putenv('GDFONTPATH=' . realpath('.'));
                for($i = 0; $i < strlen($code); $i++){
                        $color = $colors[$i % count($colors)];
                        imagettftext(
                                $this->__img,
                                24 + rand(0, 8),
                                -20 + rand(0, 40),
                                ($i + 0.3) * $this->__spacePerChar,
                                $this->__canvasHeight - 20 + rand(0, 20),
                                $color,
                                $fontName,
                                $code{$i}
                        );
                }
                #绘制随机实线
                for($i = 0; $i < 400; $i++){
                        $x1 = rand(5, $this->__canvasWidth - 5);
                        $y1 = rand(5, $this->__canvasHeight - 5);
                        $x2 = $x1 - 4 + rand(0, 8);
                        $y2 = $y1 - 4 + rand(0, 8);
                        imageline($this->__img, $x1, $y1, $x2, $y2, $this->__colors[rand(0, count($this->__colors) - 1)]);
                }
                return;
        }
        private $__img = NULL;
        private $__canvasWidth = 120;
        private $__canvasHeight = 80;
        private $__codeLength = 8;
        private $__spacePerChar = 0;
        private $__code = 12345678;
        private $__safe = 2008;
        private $__background = NULL;
        private $__borderColor = NULL;       
        private $__colors = array();
        private $__font = 'arial.ttf';
        private $__sessionName = '';
}
?>

使用方法.

<?php
#测试文件
include_once('class.safeCode.php');
session_start();
$safeCode = new safeCode;
$safeCode->setCanvas(200, 50);
$safeCode->setCodeLength(8);
$safeCode->setSafe('2008');#设置安全码,不设置有默认值,动态生成
$safeCode->setBackground();
$safeCode->setBorderColor();
$safeCode->setFont('arial.ttf');#设置字体.必须同目录,字体文件
$colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));
$safeCode->setFontColor($colors);
$safeCode->setSession();
$safeCode->display('jpeg');

?>

[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • Python爬取微信小程序通用方法代码实例详解

    这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25