搜索
首页后端开发php教程php图像处理函数实例大全及php验证码

  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
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热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

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

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)