首页 > Web开发 > 详细

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

时间:2019-08-04 21:23:54      阅读:110      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:https://www.cnblogs.com/songjianming/p/11299624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!