php中文汉字验证码生成程序

本文章提供一款php中文汉字验证码生成程序,如果在图片片生成汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧,你要产生随机数,那有mt_rand函数,你还要用到session保存这个随机数,如果需要转成utf-8,需要iconv函数.

php中文汉字验证码生成程序实例代码如下:

  1. <?php
  2. class simpleimage {
  3. var $image;
  4. var $image_type;
  5. function load($filename) {
  6. $image_info = getimagesize($filename);
  7. $this->image_type = $image_info[2];
  8. if( $this->image_type == imagetype_jpeg ) {
  9. $this->image = imagecreatefromjpeg($filename);
  10. } elseif( $this->image_type == imagetype_gif ) {
  11. $this->image = imagecreatefromgif($filename);
  12. } elseif( $this->image_type == imagetype_png ) {
  13. $this->image = imagecreatefrompng($filename);
  14. }
  15. }
  16. function save($filename, $image_type=imagetype_jpeg, $compression=75, $permissions=null) {
  17. if( $image_type == imagetype_jpeg ) {
  18. imagejpeg($this->image,$filename,$compression);
  19. } elseif( $image_type == imagetype_gif ) {
  20. imagegif($this->image,$filename);
  21. } elseif( $image_type == imagetype_png ) {
  22. imagepng($this->image,$filename);
  23. }
  24. if( $permissions != null) {
  25. chmod($filename,$permissions);
  26. }
  27. }
  28. function output($image_type=imagetype_jpeg) {
  29. if( $image_type == imagetype_jpeg ) {
  30. imagejpeg($this->image);
  31. } elseif( $image_type == imagetype_gif ) {
  32. imagegif($this->image);
  33. } elseif( $image_type == imagetype_png ) {
  34. imagepng($this->image);
  35. }
  36. }
  37. function getwidth() {
  38. return imagesx($this->image);
  39. }
  40. function getheight() {
  41. return imagesy($this->image);
  42. }
  43. function resizetoheight($height) {
  44. $ratio = $height / $this->getheight();
  45. $width = $this->getwidth() * $ratio;
  46. $this->resize($width,$height);
  47. }
  48. function resizetowidth($width) {
  49. $ratio = $width / $this->getwidth();
  50. $height = $this->getheight() * $ratio;
  51. $this->resize($width,$height);
  52. }
  53. function scale($scale) {
  54. $width = $this->getwidth() * $scale/100;
  55. $height = $this->getheight() * $scale/100;
  56. $this->resize($width,$height);
  57. }
  58. function resize($width,$height) {
  59. $new_image = imagecreatetruecolor($width, $height);
  60. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth(), $this->getheight());
  61. $this->image = $new_image;
  62. }
  63. }
  64. //开源代码phpfensi.com
  65. $newfile = upload_dir."/icons/".md5($_session['user']->email).".jpg";//上传文件保存的目录
  66. $image = new simpleimage();
  67. $image->load($_files['icons']['tmp_name']);//上传的临时文件名
  68. $image->resizetowidth(80);设置宽度
  69. $image->save($newfile);
  70. ?>