当前位置:Gxlcms > PHP教程 > php基础学习:图像处理

php基础学习:图像处理

时间:2021-07-01 10:21:17 帮助过:11人阅读

在学习php过程中会遇到图像处理的情况,本篇将会介绍图像处理的方法。

<!-- 图像处理 -->

  1. <?php
  2. // 图片处理gd2配置文件修改
  3. ?>
  4. <!-- 用图片处理函数画一张图 -->
  5. <?php
  6. // $img = imagecreatetruecolor(500, 500);
  7. // $red = imagecolorallocate($img, 255, 0, 0);
  8. // $green = imagecolorallocate($img, 0, 255, 0);
  9. // $blue = imagecolorallocate($img, 0, 0, 255);
  10. // $pur = imagecolorallocate($img, 255, 0, 255);
  11. // $yellow = imagecolorallocate($img, 121, 72, 0);
  12. // imagefilledrectangle($img, 0, 0, 500, 500, $green);
  13. // imageline($img, 0, 0, 500, 500, $red);
  14. // imageline($img, 500, 0, 0, 500, $blue);
  15. // imagefilledellipse($img, 250, 250, 200, 200, $yellow);
  16. // imagefilledellipse($img, 200, 200, 300, 300, $blue);
  17. // imagejpeg($img, 'haha.jpg');
  18. // echo "<img src='haha.jpg'>";
  19. // imagedestroy($img);
  20. ?>
  21. <!-- 开发验证码(生成验证码) -->
  22. <?php
  23. check_code();
  24. function check_code($width = 100, $height = 50,
  25. $num = 4, $type = 'jpeg'){
  26. $img = imagecreate($width, $height);
  27. $string = '';
  28. for ($i = 0;$i < $num; $i++){
  29. $rand = mt_rand(0, 2);
  30. switch ($rand) {
  31. case 0:
  32. $ascii = mt_rand(48, 57);
  33. break;
  34. case 1:
  35. $ascii = mt_rand(65, 90);
  36. break;
  37. case 2:
  38. $ascii = mt_rand(97, 122);
  39. break;
  40. }
  41. $string .= sprintf('%c', $ascii);
  42. }
  43. imagefilledrectangle($img, 0, 0, $width, $height, randBg($img));
  44. for ($i = 0;$i < 50; $i++){
  45. imagesetpixel($img, mt_rand(0, $width),
  46. mt_rand(0, $height), randPix($img));
  47. }
  48. for ($i = 0;$i < $num;$i++){
  49. $x = floor($width/$num) * $i + 2;
  50. $y = mt_rand(0, $height - 15);
  51. imagechar($img, 5, $x, $y, $string[$i], randPix($img));
  52. }
  53. $func = 'image' . $type;
  54. $header = 'Content-type:image/'.$type;
  55. if (function_exists($func)) {
  56. header($header);
  57. $func($img);
  58. }else {
  59. echo '图片类型不支持';
  60. }
  61. imagedestroy($img);
  62. return $string;
  63. }
  64. function randBg($img){
  65. return imagecolorallocate($img, mt_rand(130, 255),
  66. mt_rand(130, 255), mt_rand(130, 255));
  67. }
  68. function randPix($img){
  69. return imagecolorallocate($img, mt_rand(0, 120),
  70. mt_rand(0, 120), mt_rand(0, 120));
  71. }
  72. ?>
  73. <!-- 图像缩放和剪裁技术 -->
  74. <?php
  75. $image = imagecreatefrompng('fbb.png');
  76. $percent = 0.1;
  77. list($width, $height) = getimagesize('fbb.png');
  78. $new_width = $width * $percent;
  79. $new_height = $height * $percent;
  80. $new_image = imagecreatetruecolor($new_width, $new_height);
  81. imagecopyresampled($new_image, $image, 0, 0,
  82. 0, 0, $new_width, $new_height, $width, $height);
  83. header('content-type:image/jpeg');
  84. imagejpeg($new_image);
  85. ?>
  86. <!-- 图片水印处理 -->
  87. <?php
  88. $dst = imagecreatefrompng('https://img.php.cn/upload/course/000/
  89. 000/002/5833ebba648cf229.png');
  90. $src = imagecreatefrompng('https://img.php.cn/
  91. upload/course/000/000/002/5833ebe90cc11285.png');
  92. $dst_info = getimagesize('5833ebba648cf229.png');
  93. $src_info = getimagesize('5833ebe90cc11285.png');
  94. $dst_x = $dst_info[0] - $src_info[0];
  95. $dst_y = $dst_info[1] - $src_info[1];
  96. imagecopymerge($dst, $src, $dst_x, $dst_y, 0, 0,
  97. $src_info[0], $src_info[1], 100);
  98. header('Content-type:image/png');
  99. imagepng($dst);
  100. imagedestroy($dst);
  101. imagedestroy($src);
  102. ?>
  103. <!-- 做一个智能的图片水印函数 -->
  104. <?php
  105. ?>

本文讲解了图像处理的相关方法,更多相关内容请关注Gxl网。

相关推荐:

通过cURL来做小偷程序

php会话管理和控制

php基础学习:错误处理

以上就是php基础学习:图像处理的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行