php验证码+缩略图+饼状图+五环图
@1.验证码
1 captcher.php 2 header(‘Content-type:image/png‘); 3 session_start(); 4 $img = imagecreate(100, 30); 5 $captcha = array( 6 ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, 7 ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, 8 ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, 9 ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, 10 ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, 11 ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘); 12 $code = ‘‘; 13 for($i=0;$i<4;$i++) 14 { 15 $code.=‘ ‘.$captcha[rand(0,35)]; 16 } 17 $_SESSION[‘captcha‘] = $code; 18 $bg = imagecolorallocate ( $img , 255 , 255 , 255 ); 19 $textcolor = imagecolorallocate ( $img , 0 , 0 , 255 ); 20 imagestring($img, 5, 10, 5, $code, $textcolor); 21 imagepng($img); 22 imagedestroy($img); 23 24 captcher.html 25 <script type="text/javascript" src="jquery-2.1.1.js"></script> 26 <div style="width:100px;height:30px;" > 27 <image id="captcher" src="test.php" style="width:100px;height:30px;border:1px solid #ff0000;"/> 28 </div> 29 <script> 30 $("#captcher").click(function(){ 31 this.src = ‘test.php?id=‘+Math.random(); 32 }); 33 </script>
@2.缩略图
1 header("Content-type:image/png"); 2 function img2thumb($src_img, $dst_img, $width = 75, $height = 75, $cut = 0, $proportion = 0) 3 { 4 if(!is_file($src_img)) 5 { 6 return false; 7 } 8 $ot = fileext($dst_img); 9 $otfunc = ‘image‘ . ($ot == ‘jpg‘ ? ‘jpeg‘ : $ot); 10 $srcinfo = getimagesize($src_img); 11 $src_w = $srcinfo[0]; 12 $src_h = $srcinfo[1]; 13 $type = strtolower(substr(image_type_to_extension($srcinfo[2]), 1)); 14 $createfun = ‘imagecreatefrom‘ . ($type == ‘jpg‘ ? ‘jpeg‘ : $type); 15 16 $dst_h = $height; 17 $dst_w = $width; 18 $x = $y = 0; 19 20 /** 21 * 缩略图不超过源图尺寸(前提是宽或高只有一个) 22 */ 23 if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0)) 24 { 25 $proportion = 1; 26 } 27 if($width> $src_w) 28 { 29 $dst_w = $width = $src_w; 30 } 31 if($height> $src_h) 32 { 33 $dst_h = $height = $src_h; 34 } 35 36 if(!$width && !$height && !$proportion) 37 { 38 return false; 39 } 40 if(!$proportion) 41 { 42 if($cut == 0) 43 { 44 if($dst_w && $dst_h) 45 { 46 if($dst_w/$src_w> $dst_h/$src_h) 47 { 48 $dst_w = $src_w * ($dst_h / $src_h); 49 $x = 0 - ($dst_w - $width) / 2; 50 } 51 else 52 { 53 $dst_h = $src_h * ($dst_w / $src_w); 54 $y = 0 - ($dst_h - $height) / 2; 55 } 56 } 57 else if($dst_w xor $dst_h) 58 { 59 if($dst_w && !$dst_h) //有宽无高 60 { 61 $propor = $dst_w / $src_w; 62 $height = $dst_h = $src_h * $propor; 63 } 64 else if(!$dst_w && $dst_h) //有高无宽 65 { 66 $propor = $dst_h / $src_h; 67 $width = $dst_w = $src_w * $propor; 68 } 69 } 70 } 71 else 72 { 73 if(!$dst_h) //裁剪时无高 74 { 75 $height = $dst_h = $dst_w; 76 } 77 if(!$dst_w) //裁剪时无宽 78 { 79 $width = $dst_w = $dst_h; 80 } 81 $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1); 82 $dst_w = (int)round($src_w * $propor); 83 $dst_h = (int)round($src_h * $propor); 84 $x = ($width - $dst_w) / 2; 85 $y = ($height - $dst_h) / 2; 86 } 87 } 88 else 89 { 90 $proportion = min($proportion, 1); 91 $height = $dst_h = $src_h * $proportion; 92 $width = $dst_w = $src_w * $proportion; 93 } 94 95 $src = $createfun($src_img); 96 $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h); 97 $white = imagecolorallocate($dst, 255, 255, 255); 98 imagefill($dst, 0, 0, $white); 99 100 if(function_exists(‘imagecopyresampled‘)) 101 { 102 imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 103 } 104 else 105 { 106 imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h); 107 } 108 $otfunc($dst, $dst_img); 109 $image = imagepng($dst); 110 //print_r($dst); 111 imagedestroy($dst); 112 imagedestroy($src); 113 return $image; 114 } 115 function fileext($file) 116 { 117 return pathinfo($file, PATHINFO_EXTENSION); 118 } 119 120 $img = img2thumb(‘cj.png‘, ‘chen.png‘, 100, 100, 5, 0.6);
@3.饼状图
1 $image = imagecreatetruecolor(100, 100); 2 3 $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); 4 $gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); 5 $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); 6 $navy = imagecolorallocate($image, 0x00, 0x00, 0x80); 7 $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); 8 $red = imagecolorallocate($image, 0xFF, 0x00, 0x00); 9 $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); 10 11 for ($i=60; $i>50; $i--) { 12 imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE); 13 imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE); 14 imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE); 15 } 16 imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE); 17 imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE); 18 imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE); 19 20 header(‘Content-type: image/png‘); 21 imagepng($image); 22 imagedestroy($image);
@4.五环图
1 $size = 450; 2 $image = imagecreatetruecolor($size, 280); 3 $back = imagecolorallocate($image, 255, 255, 255); 4 $border = imagecolorallocate($image, 0, 0, 0); 5 6 imagefilledrectangle($image, 0, 0, $size-1, $size-1, $back); 7 imagerectangle($image, 0, 0, $size-1, $size-1, $border); 8 9 10 $yellow_x = 100; 11 $yellow_y = 85; 12 $red_x = $yellow_x + 120; 13 $red_y = $yellow_y; 14 $blue_x = $yellow_x + 240; 15 $blue_y = $yellow_y; 16 $radius = 150; 17 $green_x = $yellow_x +60; 18 $green_y = 180; 19 $red_low_x = $green_x + 120; 20 $red_low_y = $green_y; 21 22 $yellow = imagecolorallocatealpha($image, 255, 255, 0,75); 23 $red = imagecolorallocatealpha($image, 255, 0, 0, 75); 24 $blue = imagecolorallocatealpha($image, 0, 0, 255, 75); 25 $green = imagecolorallocatealpha($image, 0, 255, 0, 75); 26 $red_low = imagecolorallocatealpha($image, 255, 0, 255, 75); 27 28 imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow); 29 imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red); 30 imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue); 31 imagefilledellipse($image, $green_x, $green_y, $radius, $radius, $green); 32 imagefilledellipse($image, $red_low_x, $red_low_y, $radius, $radius, $red_low); 33 34 header(‘Content-Type:image/png‘); 35 36 imagepng($image); 37 imagedestroy($image);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。