• PHP利用GD库绘图和生成验证码图片


    首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下

    print_r(gd_info());

    假设有打印出内容例如以下,则说明GD功能有打开:

    Array
    (
        [GD Version] => bundled (2.0.34 compatible)
        [FreeType Support] => 1
        [FreeType Linkage] => with freetype
        [T1Lib Support] => 1
        [GIF Read Support] => 1
        [GIF Create Support] => 1
        [JPG Support] => 1
        [PNG Support] => 1
        [WBMP Support] => 1
        [XPM Support] => 
        [XBM Support] => 1
        [JIS-mapped Japanese Font Support] => 
    )
    

    GD绘图一般过程例如以下:

    1.创建一张画布资源

    2.创建颜色画笔

    3.绘图

    4.保存图片或输出图片

    5.销毁内存画布资源


    測试代码例如以下:

    <?

    php header("Content-type: image/jpeg"); $width = 400; //宽。高 $height = 400; $image = imagecreate($width, $height); //第一步:创建空白图像 $white = imagecolorallocate($image, 0, 0, 0); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色,即用 imagecreate() 建立的图像。 $green = imagecolorallocate($image, 0, 255, 0); //第二步:为图像分配颜色 imageline($image, 0, 20, 400, 20, $green); //第三步:画线 imagerectangle($image,100,40,300,100,$green); //画矩形 imagearc($image, 200, 150, 90, 90, 0, 360, $green); //画圆 imagestring($image, 14, 100, 240, "PHP is NiuBi HongHong!", $green); //写字符串 $str="abcdefghjklmnpqrstuvwxyz23456789"; $randstr = substr(str_shuffle($str), 0,4); imagestring($image, 14, 100, 260, $randstr, $green); //验证码 imagettftext($image, 14, 0, 100, 300, $green, './MSJHBD.TTF', "中文vsEnglish"); //中文验证 // imagejpeg($image,'./test.jpg'); //在当前路径下保存图片为test.jpg imagejpeg($image); //第四步:不加文件名称。直接输出到网页 imagedestroy($image); //第五步:销毁,回收资源 ?>


    測试图片例如以下:



    注:GD库强大的能够画各种报表(如柱状图,饼状图等)、缩略图、加水印图和股票走势图

    缩略图功能样例:

    <?php
    header("Content-type: image/png");
    
    $width = 300;   //原图宽。高
    $height = 210;  
    
    $thumb_width = (int)$width/2;
    $thumb_height = (int)$height/2;
    
    
    $dst = imagecreate($thumb_width,$thumb_height); //创建缩略图画布
    
    $gray = imagecolorallocate($dst, 100, 100, 100);
    
    
    $src = imagecreatefrompng('./me.png'); //读取原图
    
    //把原图copy到缩略图画布上
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); 
    
    imagepng($dst,'./me_thumb.png');
    
    imagedestroy($dst);
    imagedestroy($src);
    ?

    >



  • 相关阅读:
    使用Jenkins进行Android自动打包,自定义版本号等信息【转】
    Android config Android.mk parameter
    cocos2D-X config external library
    OpenCV Install OpenCV with Visual Studio
    cmake make library
    cocos2D-X create image sprite solution
    cocos2d-X 打开一个文件是否跨平台
    android use NDK api call AssetManager
    android add asset file
    android error: cannot use ‘throw’ with exceptions disabled
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6806371.html
Copyright © 2020-2023  润新知