php生成图片与验证码图片生成原理

这款php生成图片与验证码图片生成原理代码,是由php gd库来支持,如果你的系统不能创建图片就把gd.dll前面的;去再,重起apache,如果是iis重起iis就OK了.创建图片后记得用ImageDestroy 清空内存.

php生成图片与验证码图片实例代码如下:

  1. $w?$RESIZEWIDTH=$w:$RESIZEWIDTH=400;// 生成图片的宽度
  2. $h?$RESIZEHEIGHT=$h:$RESIZEHEIGHT=400;// 生成图片的高度
  3. function ResizeImage($im,$maxwidth,$maxheight,$name){
  4. $width = imagesx($im);
  5. $height = imagesy($im);
  6. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  7. if($maxwidth && $width > $maxwidth){
  8. $widthratio = $maxwidth/$width;
  9. $RESIZEWIDTH=true;//www.phpfensi.com
  10. }
  11. if($maxheight && $height > $maxheight){
  12. $heightratio = $maxheight/$height;
  13. $RESIZEHEIGHT=true;
  14. }
  15. if($RESIZEWIDTH && $RESIZEHEIGHT){
  16. if($widthratio < $heightratio){
  17. $ratio = $widthratio;
  18. }else{
  19. $ratio = $heightratio;
  20. }
  21. }elseif($RESIZEWIDTH){
  22. $ratio = $widthratio;
  23. }elseif($RESIZEHEIGHT){
  24. $ratio = $heightratio;
  25. }
  26. $newwidth = $width * $ratio;
  27. $newheight = $height * $ratio;
  28. if(function_exists("imagecopyresampled")){
  29. $newim = imagecreatetruecolor($newwidth, $newheight);
  30. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  31. }else{
  32. $newim = imagecreate($newwidth, $newheight);
  33. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  34. }
  35. ImageJpeg ($newim,$name);
  36. ImageDestroy ($newim);
  37. }else{
  38. ImageJpeg ($im,$name);
  39. }
  40. }
  41. if($_FILES['uploadfile']['size']){
  42. if($_FILES['uploadfile']['type'] == "image/pjpeg"){
  43. $im = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']);
  44. }elseif($_FILES['uploadfile']['type'] == "image/x-png"){
  45. $im = imagecreatefrompng($_FILES['uploadfile']['tmp_name']);
  46. }elseif($_FILES['uploadfile']['type'] == "image/gif"){
  47. $im = imagecreatefromgif($_FILES['uploadfile']['tmp_name']);
  48. }
  49. if($im){
  50. if(file_exists('bbs.jpg')){
  51. unlink('www.phpfensi.com.jpg');
  52. }
  53. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,'bbs.jpg');
  54. ImageDestroy ($im);
  55. }
  56. }
  57. //$uploadfile="bbs.jpg";