fontfile = 'XHei.ttc'; } /** * éªè¯ç å¾ççææ¹æ³ * @param int $speck_num ç»å¤å°ä¸ªç¹ * @param int $line_num ç»å¤å°æ¡çº¿ * @param int $font_num å¡«å æåæ°é * @return img $im */ public function verification($speck_num,$line_num,$font_num){ $_SESSION['ver_code'] = ''; $im = imagecreatetruecolor(100, 30); //ç»å¸ $text = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ'; //å¡«å å 容(å·²ç»å»é¤10lo) // éæºèæ¯é¢è² $red = mt_rand(110,130); $green = mt_rand(110,130); $blue = mt_rand(110,130); $back_color = imagecolorallocate($im, $red, $green, $blue); //èæ¯é¢è² $border_color = imagecolorallocate($im, 0, 114, 198); //è¾¹æ¡é¢è² imagefilledrectangle($im,0,0,100,30,$back_color); //ç»èæ¯ imagerectangle($im, 0, 0, 100, 30, $border_color); //è¾¹æ¡ // éæºçæç¹ for ($i=0; $i < $speck_num; $i++) { $red = mt_rand(0,255); $green = mt_rand(0,255); $blue = mt_rand(0,255); $x = mt_rand(0,100); $y = mt_rand(0,30); $speck_color = imagecolorallocate($im, $red, $green, $blue); //ç¹(speck:æç¹ï¼æ±¡ç¹)é¢è² imagesetpixel($im, $x, $y, $speck_color); //ç»ç¹ } // éæºçæçº¿ for ($i=0; $i < $line_num; $i++) { $red = mt_rand(0,255); $green = mt_rand(0,255); $blue = mt_rand(0,255); $cx = mt_rand(0,100); $cy = mt_rand(0,30); $width = mt_rand(0,90); $height = mt_rand(0,90); $start = mt_rand(0,360); $end = mt_rand(0,360); $line_color = imagecolorallocate($im, $red, $green, $blue); //线æ¡é¢è² imagearc($im, $cx, $cy, $width, $height, $start, $end, $line_color); //ç»çº¿ } // éæºçææå $fonts = ''; for ($i=1; $i <= $font_num; $i++) { $red = mt_rand(0,255); $green = mt_rand(0,255); $blue = mt_rand(0,255); $size = mt_rand(20,26); $angle = mt_rand(0,26); $x = 15*$i; $y = mt_rand(25,30); $font = ''; $start = mt_rand(0,(strlen($text)-1)); $font = substr($text, $start,1); $font_color = imagecolorallocate($im, 0, 0, 0); //åä½é¢è² imagefttext($im, $size, $angle, $x, $y, $font_color, $this->fontfile, $font); //å¡«å æå $fonts .= $font; } $_SESSION['ver_code'] = $fonts; return $im; } /** * å¾çç¼©æ¾æ¹æ³ * @param str $file å¾çè·¯å¾ * @param str $newFile è¦çæçæ°æä»¶ååè·¯å¾ * @param int $width è¦ç¼©æ¾ç宽度 * @param int $height è¦ç¼©æ¾çé«åº¦ * @return str $newFile 缩æ¾åçå¾çè·¯å¾ */ public function thumb($file,$newFile,$width,$height){ $old_img = imagecreatefromjpeg($file); // å¾å°å¾ççåå®½é« $old_width = imagesx($old_img); $old_height = imagesy($old_img); // å徿¯ä¾ $ratio = $old_width/$old_height; // 宽å¤ï¼é«å¤ if ($old_width >= $width and $old_height >= $height) { if ($width > $height) { $new_width = $width; $new_height = $width/$ratio; } elseif($width < $height) { $new_width = $height*$ratio; $new_height = $height; }else{ if ($ratio >= 1) { $new_width = $height*$ratio; $new_height = $height; } else { $new_width = $width; $new_height = $width/$ratio; } } if ($ratio >= 1) { $nx = -(($new_width/2) - ($width/2)); $ny = 0; } else { $nx = ($width/2) - ($new_width/2); $ny = -(($new_height/2) - ($height/2)); } } // 宽å¤ï¼é«ä¸å¤ if ($old_width >= $width and $old_height < $height) { $new_width = $width; $new_height = $width/$ratio; $nx = 0; $ny = ($height/2) - ($new_height/2); } // 宽ä¸å¤ï¼é«å¤ if ($old_width < $width and $old_height >= $height) { $new_width = $height*$ratio; $new_height = $height; $nx = ($width/2) - ($new_width/2); $ny = 0; } // 宽ä¸å¤ï¼é«ä¸å¤ if ($old_width < $width and $old_height < $height) { $new_width = $old_width; $new_height = $old_height; $nx = ($width/2) - ($new_width/2); $ny = ($height/2) - ($new_height/2); } //åå¾åæ $ox = 0; $oy = 0; //çææ°å¾ç $new_img = imagecreatetruecolor($width,$height); $back_color = imagecolorallocate($new_img, 255, 255, 255); //æ°å¾çèæ¯é¢è² imagefilledrectangle($new_img,0,0,$width,$height,$back_color); //æ°å¾çèæ¯ //å建æ°çå¾å对象(缩ç¥å¾) imagecopyresized($new_img,$old_img,$nx,$ny,$ox,$oy, $new_width,$new_height,$old_width,$old_height); // çæå¾ç imagejpeg($new_img,$newFile); // 鿝å¾ç imagedestroy($new_img); return $newFile; } /** * å¾çå æ°´å°æ¹æ³ * @param arr $img_path åå¾ä¿¡æ¯ * @param str/arr $wt_con æ°´å°å 容 * @param str $seat æ°´å°ä½ç½® 1.up_l(ä¸å·¦) 2.up_r(ä¸å³) 3.do_l(ä¸å·¦) 4.do_r(ä¸å³) * @param arr $gap è¾¹è· (width=>ä¾§è¾¹è·ç¦»ï¼height=>åºé¨è·ç¦»/é¡¶é¨è·ç¦») * @param int $alpha éæåº¦ */ public function waterMark($img_path,$wt_con,$seat,$gap,$alpha=''){ $old_path = $img_path; // å¾å°åå¾ $old_img = imagecreatefromjpeg($old_path); $old_info = getimagesize($old_path); // å¾å°æ°´å°ä¿¡æ¯(åºåå¾çææå) if (is_array($wt_con)) { $font = imagettfbbox($wt_con['size'], 0, $this->fontfile, $wt_con['text']); $wt_info['0'] = $font[4] - $font[6]; $wt_info['1'] = $font[1] - $font[7]; } else { $wt_img = imagecreatefrompng($wt_con); $wt_info = getimagesize($wt_con); } // è®¡ç®æ°´å°ä½ç½® $water_seat = $this->wmGap($old_info,$wt_info,$seat,$gap); // ææ°´å°(åºåå¾çææå) if (is_array($wt_con)) { $font_color = imagecolorallocate($old_img, 234, 234, 234); // ä¸é¢ç$water_seat['gap_h']+20 å imagettfbbox() åæ ç³»ç»åå ¶ä»çç»ä¸æè´ imagettftext($old_img, $wt_con['size'], 0, $water_seat['gap_w'], $water_seat['gap_h']+20, $font_color, $this->fontfile, $wt_con['text']); } else { if ($alpha == '') { imagecopy($old_img, $wt_img, $water_seat['gap_w'], $water_seat['gap_h'], 0, 0, $wt_info['0'], $wt_info['1']); //æ èæ¯ï¼ä¸å¯éæ } else { imagecopymerge($old_img, $wt_img, $water_seat['gap_w'], $water_seat['gap_h'], 0, 0, $wt_info['0'], $wt_info['1'], $alpha); //æèæ¯ï¼å¯éæ } } // çæå æ°´å°åçå¾ç imagepng($old_img,$old_path); // 鿝å¾ç imagedestroy($old_img); imagedestroy($wt_img); } /** * è®¡ç®æ°´å°ä½ç½® * @param arr $old_info åå¾ä¿¡æ¯ * @param arr $wt_info æ°´å°å¾ä¿¡æ¯ * @param str $seat ä½ç½® * @param int $gap è¾¹è· * @return arr $arr å¤çå¥½çæ°ç» */ public function wmGap($old_info,$wt_info,$seat,$gap){ switch ($seat) { case 'up_l': $arr['gap_w'] = $gap['0']; $arr['gap_h'] = $gap['1']; break; case 'up_r': $width = $old_info['0'] - $wt_info['0'] - $gap['0']; $arr['gap_w'] = $width; $arr['gap_h'] = $gap['1']; break; case 'do_l': $height = $old_info['1'] - $wt_info['1'] - $gap['1']; $arr['gap_w'] = $gap['0']; $arr['gap_h'] = $height; break; case 'do_r': $width = $old_info['0'] - $wt_info['0'] - $gap['0']; $height = $old_info['1'] - $wt_info['1'] - $gap['1']; $arr['gap_w'] = $width; $arr['gap_h'] = $height; break; default: $arr['gap_w'] = $gap['0']; $arr['gap_h'] = $gap['1']; break; } return $arr; } } ?>