zoukankan      html  css  js  c++  java
  • GD库 使用

    常用步骤

    创建画布 imagescreatetruecolor($width,$height) 返回资源类型

    (创建画布有两种方式1是创建一张空白的画布2是打开一张图片作为画布)

    创建各种颜料 imagecolorallocate()

    绘画 imagesfill()

    保存成图片 imagepng()

    销毁犯罪现场,销毁画布 imagedestroy()

     

    <?php
    
    /*基本使用 创建一张有填充背景的图片*/
    
    
    /*
    1:造画布(多宽,多高)  imagecreatetruecolor()
     返回是资源类型
    */
    $width = 300;
    $height = 200;
    $im = imagecreatetruecolor($width,$height);
    /*
    2:创建颜料 imagecolorallocate
    imagecolorallocate(画布资源,红,绿,蓝)
    */
    $blue = imagecolorallocate($im,0,0,255);
    /*
    3:画图
    imagefill是用颜料填充画布
    bool imagefill (画布资源 , 填充的起始点x值 , 填充的起始点y值 , 填充颜色)
    */
    imagefill($im,0,0,$blue);
    /*
    4:保存!
    imagepng
    imagejpeg
    imagegif
    ..
    来保存成不同图片格式
    */
    if(imagepng($im,'./01.png')) {
        echo '图片生成成功!';
    } else {
        echo 'fail';
    }
    /*
    5:销毁画布
    */
    imagedestroy($im);

    注意 :屏幕的坐标系 以左上角为原点,向下为正Y 向右为正X

     

    -如果要在一个图片文件中打开 :

    $file = './home.jpg';

    $im = imagecreatefromjpeg($file);

     

    -如果要直接输出到页面,则imagepng()第二个参数不要即可 ; 输出到浏览器必须声明

    header('content-type: image/png');

     

    -图片无非也是0101?只是看图软件解析了

     

    -字符串验证码的制作

    <?php
    /**
     * 制作英文验证码
     */
    
    
        // 1 造画布
        $im = imagecreatetruecolor(50,25);
        // 2浅色背景
        $gray = imagecolorallocate($im,220,220,220);
    
        // 造写字的随机颜色
        $randcolor = imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
        //造划线的随机颜色
        $linecolor1 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
        $linecolor2 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
        $linecolor3 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
    
    
        // 填充背景
        imagefill($im,0,0,$gray);
    
        // 画干扰线,值越小,颜色越浅
        imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor1);
        imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor2);
        imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor3);
    
        /*
        3 写字
        imagestring — 水平地画一行字符串
        说明
        bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
        参数分别代表: 画布资源,字体大小(1-5中选择), 字符最左上角的x坐标,y坐标 ,要写的字符串,颜色
        */
        $str = substr(str_shuffle('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz0123456789'),0,4);
        imagestring($im,5,8,5,$str,$randcolor);
        header('content-type: image/png');
        //把图片输出到调用的网页中
        imagepng($im);
    /****
    验证码的字符串还想扭曲该如何做.参考正弦曲线函数,弧度函数
    ****/

    记 : 随机颜色划线(imageline) 写字imagestring

    图点击刷新需要用js制作

    function chv(vcode) {

        //var vcode = document.getElementsByTagName('img')[0];

        vcode.src = vcode.src+'?_s='+Math.random();

    }

    onclick="chv(this);"

     

    -随机中文字符验证码

    <?php
    /***
    ====笔记部分====
    中文验证码
    
    如何产生随机的中文字符串?
    中文按其unicode编码,是有规律的,
    位于0x4E00-0x9FA0
    我们可以在uncode范围内随机选取,
    
    但是 请注意,对于用户来说,能否认得?
    因为有大量生僻字.
    
    所以在实际项目中,只是抽取几百或上千个常用汉字,放数组里,随机选取.
    ***/
    
    //举例汉字
    $char = array('','','','','','','');
    shuffle($char);
    $code = implode('',array_slice($char,0,4));
    
    
    // 画布
    $im = imagecreatetruecolor(65,25);
    
    // 颜料
    $gray = imagecolorallocate($im,200,200,200);
    $blue = imagecolorallocate($im,0,0,255);
    
    
    // 填充
    imagefill($im,0,0,$gray);
    
    // 写字
    imagettftext($im,12,0,2,20,$blue,'./msyh.ttf',$code);
    
    
    // 输出
    header('content-type: image/jpeg;');
    imagejpeg($im);
    
    //销毁
    imagedestroy($im);

    (也可以做水印)

    随机中文需要根据中文编码的规律 生成汉字

    一般是选取几百或上千的汉字

    imagettftext()

     

    -画正方形,椭圆,员,饼状图

    imagefilledarc()

    ★bool getimagesize($path) 返回值是一个数组,,获取图片的信息

     

     

  • 相关阅读:
    MYSQL索引优化法则
    关于String对象到底占多少字节?
    类加载的时机
    HotSpot 虚拟机对象探秘
    JVM 内存结构
    Tomcat10-记录踩坑
    笔记 | 吴恩达新书《Machine Learning Yearning》
    图像分割论文 | DRN膨胀残差网络 | CVPR2017
    除了边缘场景,你还能在哪里使用K3s?
    基础指南:如何在K3s中配置Traefik?
  • 原文地址:https://www.cnblogs.com/lzhlearn/p/5804960.html
Copyright © 2011-2022 走看看