目录
用户注册页面
首页 后端开发 php教程 php图像处理函数实例大全及php验证码

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

Jul 25, 2016 am 08:51 AM

  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 "

    ";  
    登录后复制
    登录后复制
  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 "

    ";  
    登录后复制
    登录后复制
  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. ?>
复制代码


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

在PHPStorm中如何进行CLI模式的调试? 在PHPStorm中如何进行CLI模式的调试? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何进行CLI模式的调试?在使用PHPStorm进行开发时,有时我们需要在命令行界面(CLI)模式下调试PHP�...

如何在系统重启后自动设置unixsocket的权限? 如何在系统重启后自动设置unixsocket的权限? Mar 31, 2025 pm 11:54 PM

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章讨论了框架中的基本安全功能,以防止漏洞,包括输入验证,身份验证和常规更新。

See all articles