Inhaltsverzeichnis
用户注册页面
Heim Backend-Entwicklung PHP-Tutorial 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 "

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


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

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Apr 06, 2025 am 12:02 AM

Die Hijacking der Sitzung kann in den folgenden Schritten erreicht werden: 1. Erhalten Sie die Sitzungs -ID, 2. Verwenden Sie die Sitzungs -ID, 3. Halten Sie die Sitzung aktiv. Zu den Methoden zur Verhinderung der Sitzung der Sitzung in PHP gehören: 1. Verwenden Sie die Funktion Session_regenerate_id (), um die Sitzungs -ID zu regenerieren. 2. Store -Sitzungsdaten über die Datenbank, 3. Stellen Sie sicher, dass alle Sitzungsdaten über HTTPS übertragen werden.

Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Apr 03, 2025 am 12:04 AM

Die Anwendung des soliden Prinzips in der PHP -Entwicklung umfasst: 1. Prinzip der Einzelverantwortung (SRP): Jede Klasse ist nur für eine Funktion verantwortlich. 2. Open and Close Principle (OCP): Änderungen werden eher durch Erweiterung als durch Modifikation erreicht. 3.. Lischs Substitutionsprinzip (LSP): Unterklassen können Basisklassen ersetzen, ohne die Programmgenauigkeit zu beeinträchtigen. 4. Schnittstellen-Isolationsprinzip (ISP): Verwenden Sie feinkörnige Schnittstellen, um Abhängigkeiten und nicht verwendete Methoden zu vermeiden. 5. Abhängigkeitsinversionsprinzip (DIP): Hoch- und niedrige Module beruhen auf der Abstraktion und werden durch Abhängigkeitsinjektion implementiert.

Wie debugge ich den CLI -Modus in PhpStorm? Wie debugge ich den CLI -Modus in PhpStorm? Apr 01, 2025 pm 02:57 PM

Wie debugge ich den CLI -Modus in PhpStorm? Bei der Entwicklung mit PHPSTORM müssen wir manchmal den PHP im CLI -Modus (COMS -Zeilenschnittstellen) debuggen ...

Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Mar 31, 2025 pm 11:54 PM

So setzen Sie die Berechtigungen von Unixsocket automatisch nach dem Neustart des Systems. Jedes Mal, wenn das System neu startet, müssen wir den folgenden Befehl ausführen, um die Berechtigungen von Unixsocket: sudo ...

Erklären Sie die späte statische Bindung in PHP (statisch: :). Erklären Sie die späte statische Bindung in PHP (statisch: :). Apr 03, 2025 am 12:04 AM

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Mar 28, 2025 pm 05:11 PM

In Artikel werden wichtige Sicherheitsfunktionen in Frameworks erörtert, um vor Schwachstellen zu schützen, einschließlich Eingabevalidierung, Authentifizierung und regelmäßigen Aktualisierungen.

See all articles