php验证码+缩略图+饼状图+五环图+获取php生成的png图标的信息+二进制与十进制和十六进制之间的转化(替换颜色值)

@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);

 @5获取php生成的png图标信息

1 <?php 
2 $img = imagecreatetruecolor(1024, 1024);   // 新建一个真彩色图像
3 $text_color = imagecolorallocate($img, 0, 255, 255); 
4 imagettftext($img, 20, 0, 150, 650, $text_color, 'arial.ttf', 'fendor陈' );   // 相对应的字体如arial.ttf要自己下载后放入本地文件中调用 
5 ob_start();            // 打开输出缓冲
6 imagepng($img);
7 $binary = ob_get_contents();  // 获取二进制码后就可以用处理图片二进制码的方式获取图标的信息了
8 ob_end_clean();
9 imagedestroy();

 @6二进制与十进制和十六进制之间的转化(替换颜色值)

 1 <?php
 2 /**
 3 * 将十六进制颜色转化为rgb格式
 4 */
 5 function hex2rgb($colour) {
 6         if ($colour [0] == '#') {
 7             $colour = substr ( $colour, 1 );
 8         }
 9         if (strlen ( $colour ) == 6) {
10             list ( $r, $g, $b ) = array ($colour [0] . $colour [1], $colour [2] . $colour [3], $colour [4] . $colour [5] );
11         } elseif (strlen ( $colour ) == 3) {
12             list ( $r, $g, $b ) = array ($colour [0] . $colour [0], $colour [1] . $colour [1], $colour [2] . $colour [2] );
13         } else {
14             return false;
15         }
16         $r = hexdec ( $r );
17         $g = hexdec ( $g );
18         $b = hexdec ( $b );
19         return $r . ',' . $g . ',' . $b;
20 }
21 
22 $color = '#eb6695';
23 $rgb = hex2rgb($color);

hexdec($str);十六进制转十进制

dechex($str):十进制转十六进制

decbin($str):十进制转二进制

bindec($str):二进制转十进制

octdec($str):八进制转十进制

decoct($str):十进制转八进制

base_convert($str, $frombase, $tobase);将$str在$frombase和$tobase之间转换

总结:hex表示十六进制,dec表示十进制, bin表示二进制,oct表示八进制

posted @ 2014-07-03 20:14  fendor  阅读(410)  评论(0编辑  收藏  举报