php gd实现简单图片验证码与图片背景文字水印
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php gd实现简单图片验证码与图片背景文字水印,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2418字,纯文字阅读大概需要4分钟。
内容图文
1、让水印文字铺满图片:
大致效果:
代码:
<?php function appendSpreadTextMark($imageDir, $markText) { $fontFile = "simsun.ttf"; $info = getimagesize($imageDir); $imWidth = $info[0]; $imHeight = $info[1]; $type = $info[2];//1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM$ext = image_type_to_extension($type, false); $mime = $info[‘mime‘]; $imgcrefunc = "imagecreatefrom".$ext; $imgRes = $imgcrefunc($imageDir); $picRangeLimit = $imHeight > $imWidth ? $imWidth : $imHeight; $fintSize = (int)($picRangeLimit / 50); if ($fintSize < 5) { $fintSize = 5; } $textColor = imagecolorallocatealpha($imgRes, 0, 0, 0, 100); $charCount = mb_strlen($markText, ‘UTF-8‘); $stepLengthX = $fintSize * 4; $stepLengthY = (int)($fintSize * $charCount * 1.2); $numX = (int)($imWidth / $stepLengthX) + 1; $numY = (int)($imHeight / $stepLengthY) + 1; $pointLeft = 0; $pointBottom = $stepLengthY; for ($inY = 0; $inY < $numY; $inY ++) { $pointLeft = 0; for ($inX = 1; $inX < $numX; $inX ++) { imagettftext($imgRes, $fintSize, 45, $pointLeft, $pointBottom, $textColor, $fontFile, $markText); $pointLeft += $stepLengthX; } $pointBottom += $stepLengthY; } header(‘content-type:‘ . $mime); $imgrespfunc = ‘image‘ . $ext; $imgrespfunc($imgRes); imagedestroy($imgRes); } $imageDir = "pic.jpg"; $markText = "水印内容"; appendSpreadTextMark($imageDir, $markText);
2、简单验证码效果:
代码:
<?php //创图像$im = @imagecreatetruecolor(500, 150) or die("Cannot Initialize new GD image stream"); //分配颜色$backgroundColor = imagecolorallocate($im, 0, 0, 0);//第一个分配的颜色默认为背景$textColor = imagecolorallocate($im, 0, 0, 255); //画像素点for ($i=0; $i<500; $i++) { imagesetpixel($im, rand(0, 500), rand(0,150), $textColor); } $textStr = ‘$im = @imagecreatetruecolor(100, 50)‘; //写字符串(原图像、字体、X坐标、Y坐标、待写字符串、字符串颜色) imagestring($im, 4, 10, 10, $textStr, $textColor); $textStr = ‘$backgroundColor = imagecolorallocate($im, 0, 0, 0)‘; imagestring($im, 4, 10, 30, $textStr, $textColor); $textStr = ‘$textColor = imagecolorallocate($im, 0, 0, 255)‘; imagestring($im, 4, 10, 50, $textStr, $textColor); $textStr = ‘imagestring($im, 5, 10, 10, $textStr, $textColor)‘; imagestring($im, 4, 10, 70, $textStr, $textColor); header("Content-type: image/png"); imagepng($im); imagedestroy($im);
原文:https://www.cnblogs.com/songjianming/p/11299624.html
内容总结
以上是互联网集市为您收集整理的php gd实现简单图片验证码与图片背景文字水印全部内容,希望文章能够帮你解决php gd实现简单图片验证码与图片背景文字水印所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。