封装调用验证码check(),thumb()缩略图,logo(

Posted on 2015-05-22 21:26  小蕊同学  阅读(207)  评论(0编辑  收藏  举报
  1. //check(); 调用验证码
  2. //thumb('图片文件',是否覆盖,宽度,高度,'重命名前缀');生成缩略图
  3. //logo('图片文件',位置,是否覆盖,'重命名前缀');添加图片水印
  4. //txt('图片文件',字号,'要添加的水印文字','水印颜色','水印透明度',位置,是否覆盖,'重命名前缀');添加文字水印
  5. //thumb('./a.jpg',false,100,100,'logo_');
  6. //logo('./a.jpg',5,false,'s_');
  7. //txt('./a.jpg',50,'育机构','rand',80,1,false,'t_');
  8. include 'inc/i.php';
  9. function check($len=4){
  10. session_start();
  11. header('content-type:image/png');
  12. $fs = ['/a.ttf','/b.ttf','/f.ttf'];
  13. $font = dirname(__FILE__).$fs[mt_rand(0,1)];
  14. $w = 35*$len;
  15. $h = 50;
  16. $i = imagecreatetruecolor($w,$h);
  17. $c = imagecolorallocatealpha($i,0,0,0,127);
  18. //imagecolortransparent($i,$c);
  19. //imagefill($i,0,0,$c);
  20. imagefilledrectangle($i,0,0,$w,$h,gc($i,'ffffff',mt_rand(0,2)));
  21. $sss = '';
  22. for($j=0;$j<$len;$j++){
  23. $st = gs(1);
  24. $sss.=$st;
  25. imagettftext($i,mt_rand(15,25),mt_rand(-30,30),$j*35+10,mt_rand(28,38),gc($i),$font,$st);
  26. }
  27. $_SESSION['code'] = $sss;
  28. imagesetthickness($i,mt_rand(2,8));
  29. for($j=0;$j<mt_rand(5,10);$j++){
  30. imagefilledarc($i,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,360),mt_rand(0,360),gc($i,'rand',mt_rand(100,120)),IMG_ARC_NOFILL);
  31. }
  32. for($j=0;$j<10;$j++){
  33. imagettftext($i,mt_rand(10,15),mt_rand(-5,5),mt_rand(0,$w),mt_rand(0,$h),gc($i,'rand',mt_rand(100,120)),$font,gs(1));
  34. }
  35. imagepng($i);
  36. imagedestroy($i);
  37. }
  38. function gs($n=4){
  39. $s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  40. $t = '';
  41. for($i=0;$i<$n;$i++){
  42. $t.=substr($s,mt_rand(0,strlen($s)-1),1);
  43. }
  44. return $t;
  45. }
  46. /**
  47. * 生成缩略
  48. */
  49. function thumb($i,$f=false,$w=220,$h=0,$fn='s_'){
  50. $ii = getimagesize($i);
  51. if($ii[2]==2){
  52. if($ii[0]>$w){
  53. $src = imagecreatefromjpeg($i);
  54. $sw = $ii[0];
  55. $sh = $ii[1];
  56. $h = $h==0 ? $w/$sw*$sh : $h;
  57. //建立新的缩略图
  58. $dst = imagecreatetruecolor($w,$h);
  59. imagecopyresampled($dst,$src,0,0,0,0,$w,$h,$sw,$sh);
  60. if($f){
  61. imagejpeg($dst,$i);
  62. }else{
  63. $path = dirname($i).'/';
  64. $name = $fn.substr($i,strrpos($i,'/')+1);
  65. imagejpeg($dst,$path.$name);
  66. }
  67. imagedestroy($dst);
  68. imagedestroy($src);
  69. }
  70. }
  71. }
  72. /**
  73. * 功能:生成水银图标,水银图标文件在inc目录中 名称 logo.png
  74. */
  75. function logo($i,$p=5,$f=true,$fn='logo_'){
  76. $ii = getimagesize($i);
  77. if($ii[2]==2){
  78. if($ii[0]>300){
  79. $ni = imagecreatefromjpeg($i);
  80. $w = $ii[0];
  81. $h = $ii[1];
  82. //水银图标 logo.png 格式
  83. $logo = dirname(__FILE__).'/logo.png';
  84. $li = imagecreatefrompng($logo);
  85. $liw = imagesx($li);
  86. $lih = imagesy($li);
  87. $x = ($w-$liw)/2;
  88. $y = ($h-$lih)/2;
  89. $pad = 35;
  90. switch($p){
  91. case 1:
  92. $x = 0+$pad;
  93. $y = 0+$pad;
  94. break;
  95. case 2:
  96. $y = 0+$pad;
  97. break;
  98. case 3:
  99. $x = $w-$liw-$pad;
  100. $y = 0+$pad;
  101. break;
  102. case 4:
  103. $x = 0+$pad;
  104. break;
  105. case 6:
  106. $x = $w-$liw-$pad;
  107. break;
  108. case 7:
  109. $x = 0+$pad;
  110. $y = $h-$lih-$pad;
  111. break;
  112. case 8:
  113. $y = $h-$lih-$pad;
  114. break;
  115. case 9:
  116. $x = $w-$liw-$pad;
  117. $y = $h-$lih-$pad;
  118. break;
  119. }
  120. imagecopy($ni,$li,$x,$y,0,0,$liw,$lih);
  121. if($f){
  122. imagejpeg($ni,$i);
  123. }else{
  124. $path = dirname($i).'/';
  125. $name = $fn.substr($i,strrpos($i,'/')+1);
  126. imagejpeg($ni,$path.$name);
  127. }
  128. imagedestroy($ni);
  129. imagedestroy($li);
  130. }
  131. }
  132. }
  133. function txt($i,$s=30,$t='版权所有',$c='rand',$a=0,$p=5,$f=true,$fn='t_'){
  134. $font = dirname(__FILE__).'/f.ttf';
  135. $ii = getimagesize($i);
  136. if($ii[2]==2){
  137. if($ii[0]>300){
  138. $ni = imagecreatefromjpeg($i);
  139. $pos = imagettfbbox($s,0,$font,$t);
  140. $pad = 30;
  141. switch($p){
  142. case 1://左上角
  143. $x = 0-$pos[0]+$pad;
  144. $y = 0-$pos[7]+$pad;
  145. break;
  146. case 2://上边 水平中央
  147. $x = ($ii[0]-$pos[2])/2;
  148. $y = 0-$pos[7]+$pad;
  149. break;
  150. case 3:
  151. $x = $ii[0]-$pos[2]-$pad;
  152. $y = 0-$pos[7]+$pad;
  153. break;
  154. case 4:
  155. $x = 0-$pos[0]+$pad;
  156. $y = ($ii[1]-$pos[6])/2;
  157. break;
  158. case 5:
  159. $x = ($ii[0]-$pos[2])/2;
  160. $y = ($ii[1]-$pos[6])/2;
  161. break;
  162. case 6:
  163. $x = $ii[0]-$pos[2]-$pad;
  164. $y = ($ii[1]-$pos[6])/2;
  165. break;
  166. case 7:
  167. $x = 0-$pos[0]+$pad;
  168. $y = $ii[1]-$pos[6]-$pad;
  169. break;
  170. case 8:
  171. $x = ($ii[0]-$pos[2])/2;
  172. $y = $ii[1]-$pos[6]-$pad;
  173. break;
  174. case 9:
  175. $x = $ii[0]-$pos[2]-$pad;
  176. $y = $ii[1]-$pos[6]-$pad;
  177. break;
  178. }
  179. imagettftext($ni,$s,0,$x,$y,gc($ni,$c,$a),$font,$t);
  180. if($f){
  181. imagejpeg($ni,$i);
  182. }else{
  183. $path = dirname($i).'/';
  184. $name = $fn.substr($i,strrpos($i,'/')+1);
  185. imagejpeg($ni,$path.$name);
  186. }
  187. imagedestroy($ni);
  188. }
  189. }
  190. }
  191. function gc($i,$c='rand',$a=0){
  192. $color = '';
  193. switch($c){
  194. case 'white':
  195. $color = imagecolorallocatealpha($i,255,255,255,$a);
  196. break;
  197. case 'black':
  198. $color = imagecolorallocatealpha($i,0,0,0,$a);
  199. break;
  200. case 'red':
  201. $color = imagecolorallocatealpha($i,255,0,0,$a);
  202. break;
  203. case 'green':
  204. $color = imagecolorallocatealpha($i,0,255,0,$a);
  205. break;
  206. case 'rand':
  207. $color = imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),$a);
  208. break;
  209. default:
  210. $cc = str_split($c,2);
  211. $color = imagecolorallocatealpha($i,hexdec($cc[0]),hexdec($cc[1]),hexdec($cc[2]),$a);
  212. break;
  213. }
  214. return $color;
  215. }





Copyright © 2024 小蕊同学
Powered by .NET 9.0 on Kubernetes