天天看點

php 圖檔幹擾線,php 驗證碼(傾斜,正弦幹擾線,黏貼,旋轉)

<?php class Utils_Caption { var $Width = ; //圖檔寬 var $Height = ; //圖檔高 var $Length = ; //驗證碼位數 var $BgColor = "#FFFFFF"; //背景色 var $TFonts = array("font ttf"); var $TFontSize=array( ); //字型大小範圍 var $TFontAngle=array( ); //旋轉角度 var $Chars = " "; //驗證碼範圍(字母數字) var $Code = array(); //驗證碼 var $Image = ""; //圖形對象 var $FontColors=array( #f # bc # bd ); //字型顔色 紅綠藍 var $TPadden = ;///字元間距 多少個字元 var $Txbase = ;///x軸兩邊距離 var $Tybase = ;///y軸兩邊距離 var $TLine =true; ///畫幹擾線 public function RandRSI() ///生成驗證碼 { $this >TFontAngle=range($this >TFontAngle[ ] $this >TFontAngle[ ]); $this >TFontSize=range($this >TFontSize[ ] $this >TFontSize[ ]); $arr=array(); $Chars=$this >Chars; $TFontAngle=$this >TFontAngle; $TFontSize=$this >TFontSize; $FontColors=$this >FontColors; $code=""; $font=dirname(__FILE__) "/font/" $this >TFonts[ ]; $charlen=strlen($Chars) ; $anglelen=count($TFontAngle) ; // 角度範圍 $fontsizelen=count($TFontSize) ; // 角度範圍 $fontcolorlen=count($FontColors) ; // 角度範圍 for($i= ;$iLength;$i++) ///得到字元與顔色 { $char=$Chars[rand( $charlen)]; ///得到字元 $angle=$TFontAngle[rand( $anglelen)]; ///旋轉角度 $fontsize=$TFontSize[rand( $fontsizelen)]; ///字型大小 $fontcolor=$FontColors[rand( $fontcolorlen)]; ///字型大小 $bound=$this >_calculateTextBox($fontsize $angle $font $char); ///得到範圍 $arr[]=array($fontsize $angle $fontcolor $char $font $bound); ///得到矩形框 $code =$char; } $this >Code=$arr; //驗證碼 return $code; } public function Draw() ///畫圖 { if(empty($this >Code)) $this >RandRSI(); $codes=$this >Code; ///使用者驗證碼 $wh=$this >_getImageWH($codes); $width=$wh[ ]; $height=$wh[ ]; ///高度 $this >Width=$width; $this >Height=$height; $this >Image = imageCreate( $width $height ); $image=$this >Image; $back = $this >_getColor ($this >_getColor( $this >BgColor)); ///背景顔色 imageFilledRectangle($image $width $height $back); ///填充背景 $TPadden=$this >TPadden; $basex=$this >Txbase; $color=null; foreach ($codes as $v) ///逐個畫字元 { $bound=$v[ ]; $color=$this >_getColor ($this >_getColor($v[ ])); imagettftext($image $v[ ] $v[ ] $basex $bound[ height ] $color $v[ ] $v[ ]); $basex=$basex+$bound[ width ]*$TPadden $bound[ left ];///計算下一個左邊距 } $this >TLine?$this >_wirteSinLine($color $basex):null; ///畫幹擾線 header("Content type: image/png"); imagepng( $image); imagedestroy($image); } private function _calculateTextBox($font_size $font_angle $font_file $text) { $box = imagettfbbox($font_size $font_angle $font_file $text); $min_x = min(array($box[ ] $box[ ] $box[ ] $box[ ])); $max_x = max(array($box[ ] $box[ ] $box[ ] $box[ ])); $min_y = min(array($box[ ] $box[ ] $box[ ] $box[ ])); $max_y = max(array($box[ ] $box[ ] $box[ ] $box[ ])); return array( left => ($min_x >= ) ? abs($min_x + ) : abs($min_x + ) top => abs($min_y) width => $max_x $min_x height => $max_y $min_y box => $box ); } private function _getColor( $color ) //#ffffff { return array(hexdec($color[ ] $color[ ]) hexdec($color[ ] $color[ ]) hexdec($color[ ] $color[ ])); } private function _getColor ( $color ) //#ffffff { return imagecolorallocate ($this >Image $color[ ] $color[ ] $color[ ]); } private function _getImageWH($data) { $TPadden=$this >TPadden; $w=$this >Txbase; $h= ; foreach ($data as $v) { $w=$w+$v[ ][ width ]*$TPadden $v[ ][ left ]; $h=$h>$v[ ][ height ]?$h:$v[ ][ height ]; } return array(max($w $this >Width) max($h $this >Height)); } //畫正弦幹擾線 private function _wirteSinLine($color $w) { $img=$this >Image; $h=$this >Height; $h =rand( ); $h =rand( ); $w =rand( ); $h =rand( ); for($i= $w/ ;$i

外帶字型

font ttf 一個簡單粗體檔案