zoukankan      html  css  js  c++  java
  • php gd实现简单图片验证码与图片背景文字水印

    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);
    
  • 相关阅读:
    Java高并发学习笔记(四):volatile关键字
    Java高并发学习笔记(三):类加载
    Java高并发学习笔记(二):线程安全与ThreadGroup
    解决druid在OpenJDK11+Gradle中的依赖问题
    解决AUR无法正常升级WebStorm2021.1.1的问题
    论软件体系架构之质量属性
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    软件架构师如何工作
    泰坦尼克号数据分析
    当当网图书数据爬取和清洗
  • 原文地址:https://www.cnblogs.com/songjianming/p/11299624.html
Copyright © 2011-2022 走看看