资讯专栏INFORMATION COLUMN

PHP_GD库

KevinYan / 2127人阅读

摘要:库画图的典型流程创建画布创建各种颜料绘画如,写字,画线,画矩形等形状保存成图片清理画布画线保存图片保存成功保存失败输出图片字母数字验证码画布中文验证码中文验证码实际项目中抽取几百个,几千个常用汉字,放数组里,随机选取的一是在了不和有

GD库画图的典型流程

创建画布

创建各种颜料

绘画(如,写字,画线,画矩形等形状)

保存成图片

清理画布

</>复制代码

字母数字验证码

</>复制代码

中文验证码

</>复制代码

扭曲验证码

</>复制代码

  1. 扭曲验证码
  2. class Image {
  3. public static function code() {
  4. $str = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789";
  5. $text = substr(str_shuffle($str), 0, 4);
  6. $src = imagecreatetruecolor(50, 25);
  7. $dst = imagecreatetruecolor(50, 25);
  8. $src_gray = imagecolorallocate($src, 200, 200, 200);
  9. $dst_gray = imagecolorallocate($dst, 200, 200, 200);
  10. imagefill($src, 0, 0, $src_gray);
  11. imagefill($dst, 0, 0, $dst_gray);
  12. $color = imagecolorallocate($src, mt_rand(50, 255), mt_rand(50, 150), mt_rand(50, 200));
  13. imagestring($src, 6, 7, 5, $text, $color);
  14. // 扭曲
  15. for ( $i=0; $i<60; $i++ ) {
  16. $offsetY = 3; // 最大波动像素 (px)
  17. $round = 2; // 2个周期 即 (4π)
  18. $posY = round(sin( ($round * 2 * M_PI / 60) * $i ) * $offsetY); // 根据正选曲线计算上下波动的 posY
  19. imagecopy($dst, $src, $i, $posY, $i, 0, 1, 25);
  20. }
  21. // 显示
  22. header("Content-type: image/png");
  23. imagepng($dst);
  24. imagedestroy($src);
  25. imagedestroy($dst);
  26. }
  27. }
  28. echo Image::code();
  29. ?>
图片处理类

水印 : 把指定的水印复制到目标上,并加透明效果

缩略图 : 把大图片复制到小尺寸画面上

图片信息

通过getimagesize() 获取图片的大小,和后缀名.

判断图片是否存在

getimagesize() 是否是有解析图片信息

处理图片的大小和后缀信息.

</>复制代码


水印

从一张图片中读取到另一张图片上,通过imagecopymerge()实现

判断图片是否存在

水印小图片是否比原始图片大

水印图的位置

处理水印图

</>复制代码

  1. $dstInfo["height"] || $waeterInfo["width"] > $dstInfo["width"] ) {
  2. return false;
  3. }
  4. // 两张图片读取到画布上, 使用处理 动态函数读取
  5. $dFun = "imagecreatefrom" . $dstInfo["ext"];
  6. $wFun = "imagecreatefrom" . $dstInfo["ext"];
  7. // 是否存在函数
  8. if ( !function_exists($dFun) || !function_exists($wFun) ) {
  9. return false;
  10. }
  11. // 动态加载函数创建画布
  12. $dIm = $dFun($dst); // 创建待操作的画布
  13. $wIm = $wFun($water); // 创建水印画布
  14. // 处理水印的位置 计算粘贴的坐标
  15. switch ($pos) {
  16. case 0: // 左上角
  17. $posX = 0;
  18. $posY = 0;
  19. break;
  20. case 1: // 右上角
  21. $posX = $dstInfo["width"] - $waeterInfo["width"];
  22. $poxY = 0;
  23. break;
  24. case 2: // 居中
  25. $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
  26. $posY = ($dstInfo["height"] - $waeterInfo["height"]) / 2;
  27. break;
  28. case 3: // 左下角
  29. $posX = 0;
  30. $posY = $dstInfo["height"] - $waeterInfo["height"];
  31. break;
  32. case 4: // 右下角
  33. $posX = $dstInfo["width"] - $waeterInfo["width"];
  34. $posY = $dstInfo["height"] - $waeterInfo["height"];
  35. break;
  36. case 5: // 底部中间
  37. $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
  38. $posY = $dstInfo["height"] - $waeterInfo["height"];
  39. break;
  40. }
  41. // 加水印
  42. imagecopymerge($dIm, $wIm, $posX, $posY, 0, 0, $waeterInfo["width"], $waeterInfo["height"], $alpha);
  43. // 保存
  44. if (!$save) {
  45. $save = $dst;
  46. unlink($dst); // 删除原图片
  47. }
  48. // 生成水印
  49. $createFun = "image" . $dstInfo["ext"];
  50. $createFun($dIm, $save);
  51. imagedestroy($dIm);
  52. imagedestroy($wIm);
  53. return true;
  54. }
  55. ?>
生成缩略图

创建画布生成缩略图,通过imagecopyresampled()

判断文件是否存在

图片信息是否为假

计算缩放比例

创建画布

计算留白和宽高

处理缩略图

</>复制代码

  1. /**
  2. * thumb 生成缩略图
  3. * 等比例缩放,两边留白
  4. * @param {String} $dst 原始路径
  5. * @param {String} $save 保存路径
  6. * @param {Int} $width 缩略图 宽度
  7. * @param {Int} $height 缩略图 高度
  8. * @return {Boolen} 生成缩略图是否成功
  9. */
  10. public static function thumb( $dst, $save=NULL, $width=200, $height=200 ) {
  11. // 判断路径是否存在
  12. if ( !file_exists($dst) ) {
  13. return false;
  14. }
  15. $dinfo = self::imageInfo($dst);
  16. // 图片信息为假
  17. if ( $dinfo == false ) {
  18. return false;
  19. }
  20. // 计算缩放比例
  21. $calc = min($width / $dinfo["width"], $height / $dinfo["height"]);
  22. // 创建原始图画布
  23. $dfunc = "imagecreatefrom" . $dinfo["ext"];
  24. $dim = $dfunc($dst);
  25. // 创建缩略画布
  26. $tim = imagecreatetruecolor($width, $height);
  27. // 创建白色填充缩略画布
  28. $while = imagecolorallocate($tim, 255, 255, 255);
  29. imagefill($tim, 0, 0, $while);
  30. // 复制并缩略
  31. $dwidth = (int)$dinfo["width"] * $calc;
  32. $dheight = (int)$dinfo["height"] * $calc;
  33. $paddingx = (int)($width - $dwidth) / 2;
  34. $paddingy = (int)($height - $dheight) / 2 ;
  35. imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $width, $height);
  36. // 保存图片
  37. if ( !$save ) {
  38. $save = $dst;
  39. unlink($dst);
  40. }
  41. $createfun = "image" . $dinfo["ext"];
  42. $createfun($tim, $save);
  43. // 销毁
  44. imagedestroy($dim);
  45. imagedestroy($tim);
  46. return true;
  47. }
  48. ?>
ImageTool.class

</>复制代码

  1. class ImageTool {
  2. /**
  3. * 分析图片的信息
  4. * @param {String} $image 图片路径
  5. * @return {mixin} Array Boolean
  6. */
  7. protected static function imageInfo( $image ) {
  8. // 判断图片是否存在
  9. if(!file_exists($image)) {
  10. return false;
  11. }
  12. $info = getimagesize($image);
  13. if($info == false) {
  14. return false;
  15. }
  16. // 此时info分析出来,是数组
  17. $img["width"] = $info[0];
  18. $img["height"] = $info[1];
  19. $img["ext"] = substr($info["mime"] ,strpos($info["mime"], "/")+1); // 后缀
  20. return $img;
  21. }
  22. /**
  23. * 加水印
  24. * @param {String} $dst 目标图片
  25. * @param {String} $water 水印小图片
  26. * @param {String} $save 存储图片位置 默认替换原始图
  27. * @param {Int} $alpha 透明度
  28. * @param {Int} $pos 水印位置
  29. * @return {Boolean} 添加水印是否成功
  30. */
  31. public static function addWater($dst, $water, $save=NULL, $pos=4, $alpha=50) {
  32. // 保证二个文件是否存在
  33. if(!file_exists($dst) || !file_exists($water)) {
  34. return false;
  35. }
  36. $dstInfo = self::imageInfo($dst); // 读取图片信息
  37. $waeterInfo = self::imageInfo($water); // 读取图片信息
  38. // 水印不能比待操作图片大
  39. if( $waeterInfo["height"] > $dstInfo["height"] || $waeterInfo["width"] > $dstInfo["width"] ) {
  40. return false;
  41. }
  42. // 两张图片读取到画布上, 使用处理 动态函数读取
  43. $dFun = "imagecreatefrom" . $dstInfo["ext"];
  44. $wFun = "imagecreatefrom" . $dstInfo["ext"];
  45. // 是否存在函数
  46. if ( !function_exists($dFun) || !function_exists($wFun) ) {
  47. return false;
  48. }
  49. // 动态加载函数创建画布
  50. $dIm = $dFun($dst); // 创建待操作的画布
  51. $wIm = $wFun($water); // 创建水印画布
  52. // 处理水印的位置 计算粘贴的坐标
  53. switch ($pos) {
  54. case 0: // 左上角
  55. $posX = 0;
  56. $posY = 0;
  57. break;
  58. case 1: // 右上角
  59. $posX = $dstInfo["width"] - $waeterInfo["width"];
  60. $poxY = 0;
  61. break;
  62. case 2: // 居中
  63. $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
  64. $posY = ($dstInfo["height"] - $waeterInfo["height"]) / 2;
  65. break;
  66. case 3: // 左下角
  67. $posX = 0;
  68. $posY = $dstInfo["height"] - $waeterInfo["height"];
  69. break;
  70. case 4: // 右下角
  71. $posX = $dstInfo["width"] - $waeterInfo["width"];
  72. $posY = $dstInfo["height"] - $waeterInfo["height"];
  73. break;
  74. case 5: // 底部中间
  75. $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
  76. $posY = $dstInfo["height"] - $waeterInfo["height"];
  77. break;
  78. }
  79. // 加水印
  80. imagecopymerge($dIm, $wIm, $posX, $posY, 0, 0, $waeterInfo["width"], $waeterInfo["height"], $alpha);
  81. // 保存
  82. if (!$save) {
  83. $save = $dst;
  84. unlink($dst); // 删除原图片
  85. }
  86. // 生成水印
  87. $createFun = "image" . $dstInfo["ext"];
  88. $createFun($dIm, $save);
  89. imagedestroy($dIm);
  90. imagedestroy($wIm);
  91. return true;
  92. }
  93. /**
  94. * thumb 生成缩略图
  95. * 等比例缩放,两边留白
  96. * @param {String} $dst 原始路径
  97. * @param {String} $save 保存路径
  98. * @param {Int} $width 缩略图 宽度
  99. * @param {Int} $height 缩略图 高度
  100. * @return {Boolen} 生成缩略图是否成功
  101. */
  102. public static function thumb( $dst, $save=NULL, $width=200, $height=200 ) {
  103. // 判断路径是否存在
  104. if ( !file_exists($dst) ) {
  105. return false;
  106. }
  107. $dinfo = self::imageInfo($dst);
  108. // 图片信息为假
  109. if ( $dinfo == false ) {
  110. return false;
  111. }
  112. // 计算缩放比例
  113. $calc = min($width / $dinfo["width"], $height / $dinfo["height"]);
  114. // 创建原始图画布
  115. $dfunc = "imagecreatefrom" . $dinfo["ext"];
  116. $dim = $dfunc($dst);
  117. // 创建缩略画布
  118. $tim = imagecreatetruecolor($width, $height);
  119. // 创建白色填充缩略画布
  120. $while = imagecolorallocate($tim, 255, 255, 255);
  121. imagefill($tim, 0, 0, $while);
  122. // 复制并缩略
  123. $dwidth = (int)$dinfo["width"] * $calc;
  124. $dheight = (int)$dinfo["height"] * $calc;
  125. $paddingx = (int)($width - $dwidth) / 2;
  126. $paddingy = (int)($height - $dheight) / 2 ;
  127. imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $width, $height);
  128. // 保存图片
  129. if ( !$save ) {
  130. $save = $dst;
  131. unlink($dst);
  132. }
  133. $createfun = "image" . $dinfo["ext"];
  134. $createfun($tim, $save);
  135. // 销毁
  136. imagedestroy($dim);
  137. imagedestroy($tim);
  138. return true;
  139. }
  140. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22068.html

相关文章

  • 用PHP读取Excel、CSV文件

    摘要:读取文件的库有很多,但用的比较多的有,现在已经不再维护了,最新的一次提交还是在年月号,建议直接使用,而且这两个项目都是同一个组织维护的,本文介绍的使用。 showImg(https://segmentfault.com/img/bVbl9a1?w=1308&h=240); PHP读取excel、csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel、PHPOff...

    hiYoHoo 评论0 收藏0
  • MixPHP 环境搭建之 Apache + PHP

    摘要:在多种环境中迁移,代码无需修改,是无缝迁移的。由于大部分用户开发是在中进行,因此开发阶段我们推荐使用部署方案,因为更简单快速,下面整体演示一下的环境搭建。安装解压至指定安装目录。先不要启动,这会启动会报错,没加环境变量。 MixPHP 是一款基于 Swoole 的常驻内存型 PHP 高性能框架。 MixPHP 同时支持多种环境中执行: Nginx + mix-httpd (使用到 S...

    Barrior 评论0 收藏0
  • MixPHP 环境搭建之 Apache + PHP

    摘要:在多种环境中迁移,代码无需修改,是无缝迁移的。由于大部分用户开发是在中进行,因此开发阶段我们推荐使用部署方案,因为更简单快速,下面整体演示一下的环境搭建。安装解压至指定安装目录。先不要启动,这会启动会报错,没加环境变量。 MixPHP 是一款基于 Swoole 的常驻内存型 PHP 高性能框架。 MixPHP 同时支持多种环境中执行: Nginx + mix-httpd (使用到 S...

    microelec 评论0 收藏0
  • 64位windows7安装apache2.4+php7.1+mysql5.7+apcu

    摘要:表示需要运行库,缺少它将会在接来下的过程中弹出类似的提示运行库下载如果以前安装过,则不必再装。回车回车回车即可将服务创建,关闭窗口。下的安装有一个临时密码问题。有空再把下的安装记录一下。 最近想更新Web服务器上的软件,查了一下apache、php、mysql版本都很高了,有些变动还很大,所以先在Win上安装熟悉一下,下面是安装配置记录: 系统:64位Windows7时间:2017年3...

    pingink 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<