php图像处理函数实例大全及php验证码

WBOY
Freigeben: 2016-07-25 08:51:45
Original
795 Leute haben es durchsucht
  1. //准备画布

  2. $im = imagecreatetruecolor(500, 300);
  3. //准备涂料

  4. $black = imagecolorallocate($im, 0, 0, 0);
  5. $white = imagecolorallocate($im, 255, 255, 255);

  6. //背景填充成黑色

  7. imagefill($im,0,0, $black);
  8. //画一个矩形,填充成白色

  9. imagefilledellipse($im, 258, 151, 200, 200, $white);
  10. //输出到浏览器或保存起来
  11. header("content-type:image/png");
  12. //输出图片
  13. imagepng($im);
  14. //关闭画布

  15. imagedestroy($im);
  16. ?>
复制代码

php图片处理函数 1,数学函数 2,图片处理函数

数学函数: 1,max(); 2,min(); 3,mt_rand();随机取一个数字

  1. echomt_rand(1,5);
  2. ?>
复制代码

mt_rand随机取一个值

  1. //随机从一个数组中取一个值

  2. $arr = array("a","b","c","d","e");
  3. $rkey = mt_rand(0,count($arr)-1);

  4. echo $arr[$rkey];

  5. ?>
复制代码

4.ceil();天花板 5.floor(); 6.round();四舍五入

  1. echo ceil(2.4);//3

  2. echo floor(2.4);//2
  3. echo round(2.4);//2
  4. echo round(2.6);//3
  5. ?>

复制代码

6.pi(); //π 取圆周率

  1. echo(pi());
  2. echo M_PI;
  3. ?>
复制代码

图片处理函数使用场景 1.验证码 2.缩放 3.裁剪 4.水印

php中穿件图像的五个步骤 1.准备画布 2.准备涂料 3.在画布上画图像或者文字 4.输出最终图像或曹村最终图像 5.释放画布资源

例子:

  1. //1.准备画布

  2. $im = imagecreatetruecolor(500,300);
  3. //2.准备涂料
  4. $black = imagecolorallocate($im, 0, 0, 0);
  5. $white = imagecolorallocate($im, 255, 255, 255);
  6. //3.在画布上画图像或者文字

  7. //如果不填充背景,默认是黑色
  8. imageellipse($im,258,151,200,200,$white);
  9. //4.输出最终图像或保存最终图像

  10. header("content-type:image/png");
  11. imagepng($im);
  12. //5.释放画布资源
  13. imagedestroy($im);
  14. ?>
复制代码

绘制图像: imagefill(); imagesetpixel();//画像素点 imageline();//画线 imagerectangle();//画一个矩形 imagepolygon();//画一个多边形 imageellipse();//画一个椭圆 imageare();画一个圆弧 imagechar();//水平的画一个字符 imagestring();//水平的画一行字符串

例子:

  1. //画线

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);
  7. //3.在画布上画图像或者文字

  8. //如果不填充背景,默认是黑色
  9. imageline($im,0,0,500,300,$white);
  10. imageline($im,0,300,500,0,$white);
  11. imageline($im,0,150,500,150,$white);
  12. imageline($im,250,0,250,300,$white);
  13. //4.输出最终图像或保存最终图像

  14. header("content-type:image/png");
  15. imagepng($im);
  16. //5.释放画布资源
  17. imagedestroy($im);
  18. ?>
复制代码

例子:

  1. //添加干扰素

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);
  7. //3.在画布上画图像或者文字

  8. //产生随机的点
  9. for ($i=0; $i
  10. imagesetpixel($im,mt_rand(0,500),mt_rand(0,300),$white);

  11. }

  12. //产生随机的线
  13. for ($j=0; $j imageline($im, mt_rand(0,500), mt_rand(0,300), mt_rand(0,500), mt_rand(0,300), $white);

  14. }//4.输出最终图像或保存最终图像
  15. header("content-type:image/png");
  16. imagepng($im);
  17. //5.释放画布资源
  18. imagedestroy($im);
  19. ?>
复制代码

例子:

  1. //画矩形:

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);
  7. //3.在画布上画图像或者文字

  8. imagerectangle($im, 20, 20, 480, 280, $white);//
  9. imagefilledrectangle($im, 20, 20, 480, 280, $white);//填充
  10. //4.输出最终图像或保存最终图像

  11. header("content-type:image/png");
  12. imagepng($im);
  13. //5.释放画布资源
  14. imagedestroy($im);
  15. ?>
复制代码

例子:

  1. //imagepolygon 画多边形_画三角形

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);
  7. //3.在画布上画图像或者文字

  8. $arr = array(
  9. 250,20,
  10. 60,240,
  11. 440,240
  12. );
  13. imagepolygon($im, $arr, 3, $white);
  14. //4.输出最终图像或保存最终图像

  15. header("content-type:image/png");
  16. imagepng($im);
  17. //5.释放画布资源
  18. imagedestroy($im);
  19. ?>
复制代码

例子,画一个3D饼状图

  1. //1.准备画布

  2. $im = imagecreatetruecolor(500,300);
  3. //2.准备涂料
  4. $black = imagecolorallocate($im, 0, 0, 0);
  5. $red = imagecolorallocate($im, 255, 0, 0);
  6. $grayred = imagecolorallocate($im, 255, 100, 100);
  7. $green = imagecolorallocate($im, 0, 255, 0);
  8. $blue = imagecolorallocate($im, 0, 0, 255);
  9. $gray = imagecolorallocate($im, 200, 200, 200);
  10. $white = imagecolorallocate($im, 255, 255, 255);
  11. //3.在画布上画图像或者文字

  12. for ($i=0; $i imagefilledarc($im, 250, 150+$i, 200, 200, 0, 70, $gray,IMG_ARC_PIE);
  13. imagefilledarc($im, 250, 150+$i, 200, 200, 70, 190, $grayred,IMG_ARC_PIE);
  14. imagefilledarc($im, 250, 150+$i, 200, 200, 190, 270, $green,IMG_ARC_PIE);
  15. imagefilledarc($im, 250, 150+$i, 200, 200, 270, 360, $blue,IMG_ARC_PIE);
  16. }

  17. imagefilledarc($im, 250, 150, 200, 200, 0, 70, $white,IMG_ARC_PIE);
  18. imagefilledarc($im, 250, 150, 200, 200, 70, 190, $red,IMG_ARC_PIE);
  19. imagefilledarc($im, 250, 150, 200, 200, 190, 270, $green,IMG_ARC_PIE);
  20. imagefilledarc($im, 250, 150, 200, 200, 270, 360, $blue,IMG_ARC_PIE);
  21. //4.输出最终图像或保存最终图像

  22. header("content-type:image/png");
  23. imagepng($im);
  24. //5.释放画布资源
  25. imagedestroy($im);
  26. ?>
复制代码

例子:

  1. //写文字:

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($im, 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255, 255, 255);
  12. //3.在画布上画图像或者文字

  13. $str= "PHP is very much";

  14. imagestring($im, 5, 260, 160, $str, $green);

  15. //4.输出最终图像或保存最终图像
  16. header("content-type:image/png");
  17. imagepng($im);
  18. //5.释放画布资源
  19. imagedestroy($im);
  20. ?>
复制代码

例子:

  1. //写单个字符:

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($im, 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255, 255, 255);
  12. //3.在画布上画图像或者文字

  13. $str= "P";

  14. imagechar($im, 5, 260, 160, $str, $green);

  15. //4.输出最终图像或保存最终图像
  16. header("content-type:image/png");
  17. imagepng($im);
  18. //5.释放画布资源
  19. imagedestroy($im);
  20. ?>
复制代码

例子,

  1. //在图片上面写字

  2. //1.准备画布
  3. $im = imagecreatetruecolor(500,300);
  4. //2.准备涂料
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($im, 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255, 255, 255);
  12. //3.在画布上画图像或者文字

  13. $str= "junzaivip";

  14. $file = "E:/PHP/fonts/SIMYOU.TTF";
  15. // $file = "fonts/SIMYOU.TTF";
  16. imagettftext($im, 50, 0, 100, 200, $green, $file, $str);

  17. //4.输出最终图像或保存最终图像

  18. header("content-type:image/png");
  19. imagepng($im);
  20. //5.释放画布资源
  21. imagedestroy($im);
  22. ?>
复制代码

PHP 验证码的设计

  1. //准备画布

  2. $im = imagecreatetruecolor(100,50);
  3. //准备涂料
  4. $black = imagecolorallocate($im, 0, 0, 0);
  5. $gray = imagecolorallocate($im, 200, 200, 200);
  6. //填充背景

  7. imagefill($im, 0, 0, $gray);
  8. //文字坐标

  9. $x = (100-4*20)/2 + 6;
  10. $y = (50-20)/2 + 20;
  11. //在画布上画图像或者文字

  12. //把三个数组联系起来

  13. $strarr = array_merge(range(1, 9),range(a, z),range(A, Z));
  14. //打乱数组

  15. shuffle($strarr);
  16. //array_slice:取数组的前几位

  17. //Join 将数组变成字符串,并且以第一个变量做分隔符
  18. $str = join('',array_slice($strarr, 0,4));
  19. $file = "E:/PHP/fonts/msyh.ttf";

  20. // $file = "fonts/msyh.ttf";
  21. imagettftext($im, 20, 0, $x, $y, $black, $file, $str);

  22. //输出最终图像或保存最终图像

  23. header("content-type:image/png");
  24. imagepng($im);
  25. //释放画布资源
  26. imagedestroy($im);
  27. ?>
复制代码

php验证码设计:这里涉及到两个页面:index.php & reg.php 说明:

这个验证码版本只实现了验证图片的动态获取 前台注册页面的验证码和生成图片的验证码进行比较 验证码是由数字 小写字母 大写字母 随机组成

index.php//实现用户的注册

  1. reg
  2. 用户注册页面


  3. 姓 名:
    密 码:
    验证码: php图像处理函数实例大全及php验证码
复制代码

reg.php//用来验证验证码是否正确

  1. session_start();

  2. // echo $_POST['username'];
  3. // echo $_POST['password'];
  4. $code = strtolower($_POST['vcode']);
  5. // echo $code;

  6. // echo "

    ";  
    Nach dem Login kopieren
    Nach dem Login kopieren
  7. // print_r($_SESSION);
  8. // echo "";
  9. $vstr = strtolower($_SESSION['vstr']);
  10. if ($code==$vstr) {

  11. //实现页面的跳转
  12. echo "<script>location='http://www.xingzuo51.com'</script>";
  13. }else{
  14. echo "<script>alert('验证码输入错误')</script>";
  15. //echo "返回注册页面";
  16. echo "<script>location='index.php'</script>";
  17. }

  18. ?>
复制代码

auth.php 用来生成验证码

  1. //开启session,开启session之前,不能有任何输出

  2. session_start();
  3. $width = 50;//验证码背景宽度
  4. $height = 26;//验证码背景高速
  5. $fonttype = 10;//验证码中字的大小
  6. //准备画布
  7. $im = imagecreatetruecolor($width,$height);
  8. //准备涂料
  9. $black = imagecolorallocate($im, 0, 0, 0);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. //填充背景

  12. imagefill($im, 0, 0, $gray);
  13. //文字坐标

  14. $x = ($width-4*$fonttype)/2 +2;
  15. $y = ($height-$fonttype)/2 + $fonttype;
  16. //在画布上画图像或者文字

  17. //把三个数组联系起来

  18. $strarr = array_merge(range(1, 9),range(a, z),range(A, Z));
  19. //打乱数组

  20. shuffle($strarr);
  21. //array_slice:取数组的前几位

  22. //Join 将数组变成字符串,并且以第一个变量做分隔符
  23. $str = join('',array_slice($strarr, 0,4));
  24. //把$str放入session中,可方便所有页面使用

  25. $_SESSION['vstr'] = $str;
  26. $file = "E:/PHP/fonts/msyh.ttf";

  27. // $file = "fonts/msyh.ttf";
  28. imagettftext($im, $fonttype, 0, $x, $y, $black, $file, $str);

  29. //输出最终图像或保存最终图像

  30. header("content-type:image/png");
  31. imagepng($im);
  32. //释放画布资源
  33. imagedestroy($im);
  34. ?>
复制代码

php验证码设计: 页面跳转: 1,php跳转

  1. $im = imagecreatefromjpeg("lyf.jpg");

  2. $x = imagesx($im);

  3. $y = imagesy($im);
  4. echo $x . $y;

  5. exit;
  6. header("content-type:image/jpeg");

  7. imagejpeg($im);
  8. ?>
复制代码

方法二获取图片的大小:

  1. $imgfile = "lyf.jpg";

  2. $imgarr = getimagesize($imgfile);

  3. echo "

    ";  
    Nach dem Login kopieren
    Nach dem Login kopieren
  4. print_r($imgarr);
  5. echo "";
  6. exit;

  7. $im = imagecreatefromjpeg("lyf.jpg");

  8. echo $x . $y;

  9. header("content-type:image/jpeg");

  10. imagejpeg($im);
  11. ?>
复制代码

图片缩放函数:

  1. $imgfile = "lyf.jpg";

  2. //为了得到大图的宽高

  3. $imgarr = getimagesize($imgfile);
  4. $maxw = $imgarr[0];

  5. $maxh = $imgarr[1];
  6. $maxt = $imgarr[2];
  7. $maxm = $imgarr['mime'];
  8. //为了把大图变为资源

  9. $im = imagecreatefromjpeg("lyf.jpg");

  10. //小图资源

  11. $minw = 100;
  12. $minh = 400;
  13. //等比例缩放
  14. if (($minw/$maxw)>($minh/$maxh)) {
  15. $rate = $minh/$maxh ;
  16. }else{
  17. $rate = $minw / $maxw ;
  18. }
  19. $minw = floor($maxw * $rate);

  20. $minh = floor($maxh * $rate);
  21. $minim = imagecreatetruecolor($minw, $minh);
  22. //把大图缩放成小图

  23. imagecopyresampled($minim, $im, 0, 0, 0, 0, $minw, $minh, $maxw, $maxh);
  24. //小图输出

  25. header("content-type:{$maxm}");
  26. //判断类型

  27. switch ($maxt) {
  28. case 1:
  29. $imageout = "imagegif";
  30. break;
  31. case 2:
  32. $imageout = "imagejpeg";
  33. break;
  34. case 3:
  35. $imageout = "imagepng";
  36. break;
  37. }

  38. $imageout($minim);

  39. $minfilename = "s_".$imgfile;
  40. $imageout($minim,$minfilename);
  41. // imagejpeg($im);
  42. //释放资源
  43. imagedestroy($maxim);
  44. imagedestroy($minim);
  45. ?>
复制代码

图片裁剪函数: imagecopyresampled();

图片水印函数: imagecopy();

3,裁剪

4,水印

  1. $maxim = imagecreatefromjpeg("lyf.jpg");

  2. $minim = imagecreatefromjpeg("lyf.jpg");
  3. $maxw = imagesx($maxim);

  4. $maxh = imagesy($maxim);
  5. $minw = imagesx($minim);

  6. $minh = imagesy($minim);
  7. imagecopy($maxim, $minim, $maxw-$minw, $maxh-$minh, 0, 0, $minw, $minh);

  8. header("content-type:image/jpeg");

  9. imagejpeg($mamim);

  10. ?>
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!