php gd图片验证,php gd实现简单图片验证码与图片背景文字水印

这篇博客分享了两个代码示例,分别是如何让水印文字铺满图片和创建简单的验证码。第一部分代码演示了使用PHP实现将指定文字均匀分布覆盖到图片上的功能,而第二部分展示了创建带有随机像素点和文字的验证码图像的步骤。这两个示例对于网站开发和安全防护具有实际应用价值。
摘要由CSDN通过智能技术生成

1、让水印文字铺满图片:

大致效果:

b_0_201908042315104482.jpg

代码:

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、简单验证码效果:

2db308a087ac0007123fcafa82633b2c.png

代码:

//创图像

$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);

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值