Rumah  >  Artikel  >  pembangunan bahagian belakang  >  GD库使用总结-1

GD库使用总结-1

WBOY
WBOYasal
2016-06-13 12:18:111099semak imbas

GD库使用小结---1

  因为一开始,“大家”都说一般任务中,用php操作图片不常见,像我们这种基本业务型的,就更用不到了,所以先别看,偶就没有看。现在有机会了自然要来玩一把。

  以前学过C#的GDI+,交了课程设计后忘得一干二净。又被迫学了点MFC的画图,觉得这是最蛋疼的画图过程。去年做了个小任务时用到了js图表控件,用的是封装好的js库直接调方法,然后发现这是用HTML5的canvas元素结合js打造而成,这些chart控件很多很漂亮:jsChart、HighChart、EChart、aChart、Chart.js等等,都是基于canvas,还有svg这货也很强大。

  对比下有不少共同点,首先都要创建一个环境(context),就像真实的人画画一样,得给他一个完整的环境:一间画室,一张画板,若干画笔、颜料等,这样的硬性条件具备后才能开始作图,这个环境听起来很抽象,简单理解就是机子上必须具备某些“库”、“扩展”,类似于.net framework、jave runtime等,画js图表你得下载和加载对应的js文件,你的浏览器得支持canvas,这些硬性条件构成了这个(底层)环境,某些任务需要这些底层去完成,就像玩游戏,很多大点的游戏必须要directX,也是这个道理。

  然后就是调用这个环境提供的各种库的方法,来画各种图形:点、线、圆(弧)、椭圆、矩形、多边形、字符、文本等,再强大的可能直接提供了一些经典的曲线如贝塞尔等,然后可以设置这些图形的样式,线条连续还是断续,颜色红还是绿,画图形时是画个空框架还是将内部填充,画在界面上哪里(图在屏幕上坐标多少),正着放还是歇着放(角度多少),图形长宽如何等等,都应有方法可调。

  等一切设置完毕就是画到界面上了,然后可能会做释放某些资源的扫尾工作(垃圾回收)。大多数工作都在设置各种参数的阶段。

  php使用GD库进行绘图,请确保开启了这个扩展。可以看到当前GD库的版本,支持的类型GIF、JPEG、PNG、WBMP等,FreeType大致查了下是一个字体引擎,提供访问各种字体接口,可供我们绘制文字,也算一个库扩展,只是包含在gd库中。

      

  php画图也基本遵循这个过程:创建画布(相当于绘图环境)->设置参数,绘制图像 -> 输出(保存)图像 -> 资源释放。如果是将图片直接输出到浏览器,实际是放在一个img标签中,它的src属性是这个脚本的所在路径。一个最简单的示例

<span style="color: #000000;">php    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建画布</span>    <span style="color: #800080;">$width</span> = 200;   <span style="color: #008000;">//</span><span style="color: #008000;"> 规定画布的宽高</span>    <span style="color: #800080;">$height</span> = 100<span style="color: #000000;">;    </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 创建一幅真彩色图像    // 添加一些即将用到的颜色</span>    <span style="color: #800080;">$white</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xf2, 0xec, 0xe0<span style="color: #000000;">);    </span><span style="color: #800080;">$orange</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xa5, 0x4c<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 对画布背景填充颜色</span>    imagefill(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$white</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一串字符串在画布上</span>    imagestring(<span style="color: #800080;">$image</span>, 5, 30, 40, "Hello World", <span style="color: #800080;">$orange</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 通知浏览器输出的是图像(png类型)</span>    <span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出到浏览器</span>    imagepng(<span style="color: #800080;">$image</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放图像资源</span>    imagedestroy(<span style="color: #800080;">$image</span>);

   运行结果与审查元素,src是脚本访问链接,图片实际是放在img标签中。

        

  1. 创建画布

  在php中有三种方式,一是imagecreate方法,一是imagecreatetruecolor,这两者不依赖已有文件,创建一幅画布,参数需要制定画布的宽高(如示例),返回一个resource资源型变量,有的称为句柄,以后的操作都在这个资源型变量上进行,它就代表这这幅图像,imagesx和imagesy方法可分别获取到它的宽高属性。稍不同的是,imagecreate方法会在添加颜色时,将第一种添加的颜色(调用imagecolorallocate方法)作为整个画布的背景色,而后者则需要调用方法对背景指定一个添加好的颜色进行填充。还有一类方法是基于现有的文件来创建画布,如imagecreatefromjpeg方法,从一个现有的jpeg(jpg)格式图片创建画布,参数需要你传入该格式文件所在的路径,所以它的代码可以简单到这样

<span style="color: #000000;">php    </span><span style="color: #800080;">$filename</span> = 'H:\wampserver\wamp\www\html\image\2015.png';  <span style="color: #008000;">//</span><span style="color: #008000;"> 一个有效的文件路径</span>    <span style="color: #800080;">$image</span> = imagecreatefrompng(<span style="color: #800080;">$filename</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 从现有文件创建图像</span>    <span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);    imagepng(</span><span style="color: #800080;">$image</span>);                        <span style="color: #008000;">//</span><span style="color: #008000;"> 输出图像到浏览器</span>    imagedestroy(<span style="color: #800080;">$image</span>);                    <span style="color: #008000;">//</span><span style="color: #008000;"> 释放资源</span>

  2. 设置颜色各项参数

  GD库绘图时颜色是被分配(allocate)到画布环境中的,不是你想画个啥,临时给个颜色变量就行,调用的是imagecolorallocate方法,原型是

  int imagecolorallocate ( resource $image , int $red , int $green , int $blue ),第一个参数是前面创建的图像资源变量,后三个一次是红绿蓝色值,采用RGB三原色佳色法原理,绘制真彩色图像,每一个范围是0~255,当RGB=(255, 255, 255),就是白色,很多地方喜欢用十六进制表示,比如CSS中常见#ffffff(大小写均可),所以R、G、B可分别给值0xff、0xff、0xff,每allocate一次分配一种。看看imagecolorallocate函数原型,你会发现它返回的是int型,所以推测它只是对传入的rgb值合成一个颜色后返回一个编号,rgb真彩色是65535种颜色,这样可以每个颜色对应一个数值编号,最好当然还是用变量,凭空写个颜色数值恐怕很难。

  使用imagecreate创建画布时,会将第一种分配的颜色作为画布的背景色(如前),如果是imagecreatetruecolor,则需调用imagefill来填充背景色,方法原型:bool imagefill ( resource $image , int $x , int $y , int $color ),它会将$x和$y附近的点填充为$color颜色,所以使用imagefill的话,基本上要么将背景全部填充为一种颜色,要么不填充(默认黑色)。但是一张画布上作画时,也许你需要背景色,但很可能不会将整张画板都涂上一种背景色,旁边总得留点白纸的地方,所以GD库提供了能将背景填充成各种具体形状的方法,如矩形(imagefilledrectangle)、椭圆(imagefilledecllipse)、圆形(椭圆)或扇形(imagefilledarc)、任意多边形(imagefilledpolygon)等,以椭圆形为例,方法原型:bool imagefilledellipse($image, $cx, $cy, $width, $height, $color),第一个参数资源变量,第二、三个椭圆圆心坐标,四、五设置椭圆宽高度,最后是颜色,简单如此,注意椭圆的宽高度指长轴长度、短轴长度,而非长半轴、短半轴长度

<span style="color: #000000;">php    </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(200, 100<span style="color: #000000;">);    </span><span style="color: #800080;">$background_color</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0x13, 0x71, 0x32<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 填充背景为矩形</span>    imagefilledellipse(<span style="color: #800080;">$image</span>, 100, 50, 200, 100, <span style="color: #800080;">$background_color</span><span style="color: #000000;">);    </span><span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);    imagepng(</span><span style="color: #800080;">$image</span><span style="color: #000000;">);    imagedestroy(</span><span style="color: #800080;">$image</span>);

      

  3.绘制图像

  实际上第2步中的背景色填充就是在绘制图像,只是默认的黑色背景不好看,所以可以先给它填充个另外的颜色。绘图时也讲究个坐标、宽高的问题。老样子,屏幕坐标原点在左上角,水平往右X值学大,竖直向下Y值越大,以像素为单位。除了imagefill外还有:

    像素点:imagesetpixel  

    直线:imageline

    矩形:imagerectangle

    多边形:imagepolygon

    椭圆:imageellipse

    圆弧:imagearc

  以上的矩形、多边形、椭圆均有对应的填充类型函数,即它们只是画一个简单的形状边线。一段测试代码

<span style="color: #000000;">php    </span><span style="color: #800080;">$width</span> = 200<span style="color: #000000;">;    </span><span style="color: #800080;">$height</span> = 450<span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建图像,得到一个资源变量</span>    <span style="color: #800080;">$img</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 分配颜色</span>    <span style="color: #800080;">$orange</span> = imagecolorallocate(<span style="color: #800080;">$img</span>, 0xff, 0x66, 0x33<span style="color: #000000;">);     </span><span style="color: #800080;">$creamy_white</span> = imagecolorallocate(<span style="color: #800080;">$img</span>, 0xe8, 0xe9, 0xdb<span style="color: #000000;">);     </span><span style="color: #800080;">$blue</span> = imagecolorallocate(<span style="color: #800080;">$img</span>, 0x1e, 0x90, 0xff<span style="color: #000000;">);    </span><span style="color: #800080;">$green</span> = imagecolorallocate(<span style="color: #800080;">$img</span>, 0x64, 0xb1, 0x67<span style="color: #000000;">);    </span><span style="color: #800080;">$red</span> = imagecolorallocate(<span style="color: #800080;">$img</span>, 0xf6, 0x65, 0x78<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 填充背景</span>    imagefill(<span style="color: #800080;">$img</span>, 0, 0, <span style="color: #800080;">$creamy_white</span><span style="color: #000000;">);      </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画线</span>    imageline(<span style="color: #800080;">$img</span>, 10, 10, 100, 60, <span style="color: #800080;">$orange</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一个填充的矩形</span>    imagefilledrectangle(<span style="color: #800080;">$img</span>, 10, 70, 160, 150, <span style="color: #800080;">$blue</span><span style="color: #000000;">);     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画多边形</span>    <span style="color: #800080;">$point_coords</span> = <span style="color: #0000ff;">array</span>(10, 160, 50, 170, 120, 270, 5, 250<span style="color: #000000;">);    imagepolygon(</span><span style="color: #800080;">$img</span>, <span style="color: #800080;">$point_coords</span>, <span style="color: #008080;">count</span>(<span style="color: #800080;">$point_coords</span>)/2, <span style="color: #800080;">$green</span><span style="color: #000000;">);     </span><span style="color: #008000;">//</span><span style="color: #008000;">imagefilledpolygon($img, $point_coords, count($point_coords)/2, $green); // 画一个填充多边形    // 画一段圆弧</span>    imagearc(<span style="color: #800080;">$img</span>, 80, 310, 80, 80, 0, 240, <span style="color: #800080;">$orange</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画一个填充的椭圆</span>    imagefilledellipse(<span style="color: #800080;">$img</span>, 80, 400, 150, 70, <span style="color: #800080;">$red</span><span style="color: #000000;">);        </span><span style="color: #008080;">header</span>('Content-Type:image/png'<span style="color: #000000;">);    imagepng(</span><span style="color: #800080;">$img</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 生成图像</span>    imagedestroy(<span style="color: #800080;">$img</span>);  <span style="color: #008000;">//</span><span style="color: #008000;"> 释放资源</span>

  效果

    

  例如,画直线方法原型:bool imageline(resource $image, int $x1, int $y1, int $x2, int $y2, int $color),x1,y1和x2,y2是线的起点和终点坐标,画直线就是从这个点到哪个点。填充矩形方法中,x1,y1和x2,y2分别对应矩形的左上角和右下角坐标。多边形的方法:bool imagepolygon(resource $image, array $points, int $num_points, int $color),第二个参数$points是个一维数组,里边放着多边形各个顶点的坐标值,最好按照顺时针或逆时针围着多边形写,不会遗漏,第三个参数$num_points是顶点个数,所以是第二个坐标值数组的总数除2,这点要注意。画圆弧imagearc这里是画个弧线,也有对应的imagefilledarc可以画一个填充的饼状弧形。对于画椭圆imagefilledellipse(resource $image, int $cx, int $cy, int $width, int $height, int $color),$cx、$cy是椭圆中心坐标,$width、$height是指长轴长(宽)、短轴长(高)。

  4. 绘制文字字符串

  除了画这些图形,gd库允许我们画单个字符和字符串,比如水平画一个字符串方法:bool imagestring(resource $image, int $font, int $x, int $y, string $s, int $col),$image是图像句柄,$font是字体类型,gd内置五种字体(貌似除了大小外都差不多)。用int值标识它们(1至5,值越大字越大),传入其中一个即可,$x、$y是坐标值,指一串字符串的左上角的坐标(参考前面例子)。既然强调了水平画一个字符串,不出意外就有竖直方向的,确实有:imagestringup,对于画单个字符同理:imagechar和imagecharup。绘制字符能干什么,最经典的莫过于验证码了,试着写了个简单的验证码类

<span style="color: #000000;">php    </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Validcode    {        </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$codeNum</span> = 4;   <span style="color: #008000;">//</span><span style="color: #008000;"> 验证码个数</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$width</span> = 80;    <span style="color: #008000;">//</span><span style="color: #008000;"> 图像宽度</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$height</span> = 20;    <span style="color: #008000;">//</span><span style="color: #008000;"> 图像高度</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$image</span> = <span style="color: #0000ff;">false</span>;        <span style="color: #008000;">//</span><span style="color: #008000;"> 图像句柄</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span>();    <span style="color: #008000;">//</span><span style="color: #008000;"> 颜色色值</span>        <span style="color: #0000ff;">private</span> <span style="color: #800080;">$vcodes</span> = '';        <span style="color: #008000;">//</span><span style="color: #008000;"> 验证码字符串</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$instance</span> = <span style="color: #0000ff;">null</span>;    <span style="color: #008000;">//</span><span style="color: #008000;"> 当前实例</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$num</span> = 4, <span style="color: #800080;">$width</span> = 80, <span style="color: #800080;">$height</span> = 20<span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">extension_loaded</span>('gd'<span style="color: #000000;">))            {                </span><span style="color: #0000ff;">exit</span>('fatal error: no gd extension!'<span style="color: #000000;">);            }            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 规定字符数为3到5个</span>            <span style="color: #0000ff;">if</span>((int)<span style="color: #800080;">$num</span> $num > 5<span style="color: #000000;">)             {                </span><span style="color: #800080;">$num</span> = 4<span style="color: #000000;">;            }            </span><span style="color: #800080;">$this</span>->codeNum = (int)<span style="color: #800080;">$num</span><span style="color: #000000;">;            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 限制宽高</span>            <span style="color: #800080;">$min_width</span> = (imagefontwidth(5) * <span style="color: #800080;">$this</span>->codeNum) + 10<span style="color: #000000;">;            </span><span style="color: #800080;">$min_height</span> = imagefontheight(5) + 10<span style="color: #000000;">;            </span><span style="color: #800080;">$width</span> $min_width && <span style="color: #800080;">$width</span> = <span style="color: #800080;">$min_width</span><span style="color: #000000;">;            </span><span style="color: #800080;">$height</span> $min_height && <span style="color: #800080;">$height</span> = <span style="color: #800080;">$min_height</span><span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->width = <span style="color: #800080;">$width</span><span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->height = <span style="color: #800080;">$height</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取单例</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span>  <span style="color: #0000ff;">function</span> getInstance(<span style="color: #800080;">$num</span> = 4, <span style="color: #800080;">$width</span> = 80, <span style="color: #800080;">$height</span> = 20<span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(self::<span style="color: #800080;">$instance</span> === <span style="color: #0000ff;">null</span><span style="color: #000000;">)            {                self</span>::<span style="color: #800080;">$instance</span> = <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$num</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span><span style="color: #000000;">);            }            </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$instance</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 获取图像句柄         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getImage()        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->image === <span style="color: #0000ff;">false</span><span style="color: #000000;">)                </span><span style="color: #800080;">$this</span>->image = imagecreatetruecolor(<span style="color: #800080;">$this</span>->width, <span style="color: #800080;">$this</span>-><span style="color: #000000;">height);            </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>-><span style="color: #000000;">image)                </span><span style="color: #0000ff;">exit</span>('can not initialize image resource!'<span style="color: #000000;">);            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加一个默认背景色到数组</span>            self::<span style="color: #800080;">$colors</span>['yello_back'] = imagecolorallocate(<span style="color: #800080;">$this</span>->image, 0xf5, 0xf5, 0xdc<span style="color: #000000;">);        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*          * 添加颜色         * @param color array('colorKey'=>array(r,g,b),)         * 注意格式参数,array(键名=>array(r值, g值, b值),),可一次添加多个         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addColors(<span style="color: #800080;">$colors</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">())        {            </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$colors</span><span style="color: #000000;">))            {                </span><span style="color: #0000ff;">echo</span> 'ERROR: pass array of colors data. LINE:'.<span style="color: #ff00ff;">__LINE__</span>.'<br>'<span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            }            </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>-><span style="color: #000000;">image)            {                    </span><span style="color: #0000ff;">echo</span> 'ERROR: uninilitialized image resource. LINE:'.<span style="color: #ff00ff;">__LINE__</span>.'<br>'<span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            }            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 批量添加颜色    </span>            <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$colors</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$colKey</span>=><span style="color: #800080;">$colVal</span><span style="color: #000000;">)            {                </span><span style="color: #0000ff;">if</span>(!<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$colVal</span>) || <span style="color: #008080;">count</span>(<span style="color: #800080;">$colVal</span>) )                    <span style="color: #0000ff;">continue</span><span style="color: #000000;">;                </span><span style="color: #800080;">$rgb</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();                </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$colVal</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$val</span><span style="color: #000000;">)                {                    </span><span style="color: #800080;">$rgb</span>[] = (int)<span style="color: #800080;">$val</span><span style="color: #000000;">;                }                self</span>::<span style="color: #800080;">$colors</span>[<span style="color: #800080;">$colKey</span>] = imagecolorallocate(<span style="color: #800080;">$this</span>->image, <span style="color: #800080;">$rgb</span>[0], <span style="color: #800080;">$rgb</span>[1], <span style="color: #800080;">$rgb</span>[2<span style="color: #000000;">]);            }            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 设置背景色,请保证$x,$y坐标位于画布内         * @param colorName string 添加过的颜色对应的名称         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> fillBackgroundColor(<span style="color: #800080;">$x</span> = 0, <span style="color: #800080;">$y</span> = 0, <span style="color: #800080;">$colorName</span> = 'yello_back'<span style="color: #000000;">)        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->image && <span style="color: #0000ff;">isset</span>(self::<span style="color: #800080;">$colors</span>[<span style="color: #800080;">$colorName</span><span style="color: #000000;">]))            {                </span><span style="color: #0000ff;">return</span> imagefill(<span style="color: #800080;">$this</span>->image, <span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, self::<span style="color: #800080;">$colors</span>[<span style="color: #800080;">$colorName</span><span style="color: #000000;">]);            }            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">            {                </span><span style="color: #0000ff;">echo</span> 'ERROR: fill background color failed. LINE:'.<span style="color: #ff00ff;">__LINE__</span>.'<br>'<span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            }        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 创建随机验证码         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> createVCode()        {            </span><span style="color: #800080;">$chars</span> = '23456789'.'abcdefghjkmn'.                     'pqrstuvwxyz'.'ABCDEFG'.                     'HJKLMNPQRSTUVWXYZ'<span style="color: #000000;">;            </span><span style="color: #800080;">$this</span>->vcodes = ''<span style="color: #000000;">;            </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->codeNum; <span style="color: #800080;">$i</span>++<span style="color: #000000;">)            {                </span><span style="color: #800080;">$this</span>->vcodes .= <span style="color: #800080;">$chars</span>[<span style="color: #008080;">mt_rand</span>(0, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$chars</span>)-1<span style="color: #000000;">)];            }            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 在画布上画出验证码         * 字体的大小、颜色、位置随机变化         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> drawVCode()        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->image === <span style="color: #0000ff;">false</span><span style="color: #000000;">)                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">vcodes)            {                </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $this->codeNum; <span style="color: #800080;">$i</span>++<span style="color: #000000;">)                {                    </span><span style="color: #800080;">$font_size</span> = <span style="color: #008080;">mt_rand</span>(3, 5<span style="color: #000000;">);                     </span><span style="color: #800080;">$x</span> = <span style="color: #800080;">$i</span> * <span style="color: #008080;">floor</span>(<span style="color: #800080;">$this</span>->width / <span style="color: #800080;">$this</span>->codeNum) + 2<span style="color: #000000;">;                    </span><span style="color: #800080;">$y</span> = <span style="color: #008080;">mt_rand</span>(0, <span style="color: #800080;">$this</span>->height-imagefontheight(<span style="color: #800080;">$font_size</span><span style="color: #000000;">));                    </span><span style="color: #800080;">$color</span> = imagecolorallocate(<span style="color: #800080;">$this</span>->image, <span style="color: #008080;">mt_rand</span>(0, 128), <span style="color: #008080;">mt_rand</span>(0, 128), <span style="color: #008080;">mt_rand</span>(0, 128<span style="color: #000000;">));                    imagechar(</span><span style="color: #800080;">$this</span>->image, <span style="color: #800080;">$font_size</span>, <span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$this</span>->vcodes[<span style="color: #800080;">$i</span>], <span style="color: #800080;">$color</span><span style="color: #000000;">);                }                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;            }            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 绘制图像         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> generateImage()        {            </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>-><span style="color: #000000;">image)            {                </span><span style="color: #0000ff;">echo</span> 'ERROR: invalid image resource. LINE: '.<span style="color: #ff00ff;">__LINE__</span>.'<br>'<span style="color: #000000;">;                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;            }             </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取当前GD库支持的图片类型,绘制图像</span>            <span style="color: #0000ff;">if</span>(imagetypes() &<span style="color: #000000;"> IMG_PNG)            {                </span><span style="color: #008080;">header</span>('Content-Type: image/png'<span style="color: #000000;">);                imagepng(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(imagetypes() &<span style="color: #000000;"> IMG_JPG)            {                </span><span style="color: #008080;">header</span>('Content-Type: image/jpg'<span style="color: #000000;">);                imagejpeg(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(imagetypes() &<span style="color: #000000;"> IMG_GIF)            {                </span><span style="color: #008080;">header</span>('Content-Type: image/gif'<span style="color: #000000;">);                imagegif(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(imagetypes() &<span style="color: #000000;"> IMG_WBMP)            {                </span><span style="color: #008080;">header</span>('Content-Type: image/vnd.wap.wbmp'<span style="color: #000000;">);                imagejpeg(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);            }            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">            {                </span><span style="color: #0000ff;">exit</span>('this version do not support creating image!'<span style="color: #000000;">);            }        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 输出图像         * @param colorName type string|array('key'=>array(int, int, int),)         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> outputImage(<span style="color: #800080;">$color</span> = 'yello_back', <span style="color: #800080;">$x</span> = 0, <span style="color: #800080;">$y</span> = 0<span style="color: #000000;">)        {            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 初始化图像资源变量</span>            <span style="color: #800080;">$this</span>-><span style="color: #000000;">getImage();            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建验证码字符串     </span>            <span style="color: #800080;">$this</span>-><span style="color: #000000;">createVCode();              </span><span style="color: #008000;">//</span><span style="color: #008000;"> 填充背景色 </span>            <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_string</span>(<span style="color: #800080;">$color</span>) && <span style="color: #0000ff;">isset</span>(self::<span style="color: #800080;">$colors</span>[<span style="color: #800080;">$color</span>])) <span style="color: #008000;">//</span><span style="color: #008000;">传入字符串时判断已有颜色数组中是否有</span><span style="color: #000000;">            {                </span><span style="color: #800080;">$this</span>->fillBackgroundColor(<span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$color</span><span style="color: #000000;">);            }            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$color</span>) && <span style="color: #008080;">count</span>(<span style="color: #800080;">$color</span>) == 1 && <span style="color: #800080;">$this</span>->addColors(<span style="color: #800080;">$color</span>)) <span style="color: #008000;">//</span><span style="color: #008000;"> 传入数组进行添加和填充,只能传入一种颜色</span><span style="color: #000000;">            {                </span><span style="color: #800080;">$colorName</span> = <span style="color: #008080;">array_keys</span>(<span style="color: #800080;">$color</span><span style="color: #000000;">);                </span><span style="color: #800080;">$this</span>->fillBackgroundColor(<span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$colorName</span>[0<span style="color: #000000;">]);            }            </span><span style="color: #0000ff;">else</span>      <span style="color: #008000;">//</span><span style="color: #008000;"> 否则使用默认背景色填充</span><span style="color: #000000;">            {                </span><span style="color: #800080;">$this</span>->fillBackgroundColor(<span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, 'yello_back'<span style="color: #000000;">);            }            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 画验证码字符</span>            <span style="color: #800080;">$this</span>-><span style="color: #000000;">drawVCode();            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成图像</span>            <span style="color: #800080;">$this</span>-><span style="color: #000000;">generateImage();            </span><span style="color: #0000ff;">return</span> ''<span style="color: #000000;">;        }        </span><span style="color: #008000;">/*</span><span style="color: #008000;">*         * 释放资源         </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct()        {            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">image)                imagedestroy(</span><span style="color: #800080;">$this</span>-><span style="color: #000000;">image);        }    }    </span><span style="color: #008000;">//</span><span style="color: #008000;"> test1: 默认4个字符</span>    <span style="color: #800080;">$vcodeObj</span> = Validcode::<span style="color: #000000;">getInstance();    </span><span style="color: #800080;">$vcodeObj</span>->outputImage();*/     <span style="color: #008000;">/*</span><span style="color: #008000;">    // test2: 3个字符    $vcodeObj = Validcode::getInstance(3);    $vcodeObj->outputImage(); // 3个字符</span><span style="color: #008000;">*/</span>    <span style="color: #008000;">/*</span><span style="color: #008000;">    // test3: 设置成5个字符,且图片长宽不合理,使用对象内部调节    $vcodeObj = Validcode::getInstance(5, 1, 1);    // 输出时传入颜色值,替换背景色    $vcodeObj->outputImage(array('col'=>array(255, 255, 0)));    </span><span style="color: #008000;">*/</span>

  三个测试输出的结果:

        

  效果还行,首先是构造函数,来设置长宽和生成验证码个数,个数定为3到5个,长宽设置最小的话要容得下几个字符的宽度(测试3),否则就有显示不完全了,$codeNum为验证码个数,$weight、$height为图像宽高,$image为图像句柄变量,$colors是一个数组存放可能用到的背景色,$vcodes是验证码字符串。getImage方法获取图像句柄,并添加一个默认的颜色值,作为备用。addColors方法可批量添加颜色,fillBackgroundColor执行填充图像背景色,createVCode创建随机验证码,大小写、数字随机组合,drawVCode在画布上画出验证码,这里要保证验证均匀分布在画布上,上下高度随机,颜色随机。generateImage根据当前支持的图片类型,设置头信息,参考官网手册的示例代码。

  最后一个outputImage接口是将以上方法组合,第一个参数允许在输出验证码时用传递的色值代替当前备用的背景色,也可传入字符串名,在背景色中寻找对应色值数组并填充,当然addColors是公有的接口,也可先调用它添加你的背景色值,再调fillBackgroundColor填充,再调用outputImage传入对应颜色键名即可。需要注意的是,fillBackgroundColor方法填充背景色时,坐标值$x、$y一定要调整在画布内,否则填充无效,这里只是简单演示,图片位于浏览器左上角,所以直接是(0,0),放到页面其他位置的话注意计算坐标,这里是尽量图简单了。

  不仅是验证码了,图片加水印也可以这么干,一个道理~

  前面输出英文字符串后,自然而然,我们就可以想到能否绘制中文,确实可以。前面说了GD库中有个FreeType的字体引擎,而php提供了imagefttext和imagettf两个方法前者是

 将FreeType 2 字体写入图像,后者是将TrueType字体写入图像,查了下,这个TrueType算是FreeType的子成员,所以前者所支持的字体库更多,用它更有保证。实际上Win上面自带有字体,在C:\Windows\Fonts(C盘为安装盘时)下面,好像不是每种都能拿来用,选了一个黑体玩一下

<span style="color: #000000;">php        </span><span style="color: #800080;">$width</span> = 300; <span style="color: #800080;">$heiht</span> = 300<span style="color: #000000;">;    </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$heiht</span><span style="color: #000000;">);    </span><span style="color: #800080;">$yellow</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xe6, 0xa4<span style="color: #000000;">);    </span><span style="color: #800080;">$blue</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0x40, 0x99, 0xff<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 填充背景</span>    imagefilledrectangle(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$heiht</span>, <span style="color: #800080;">$yellow</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 字体文件    //$fontfile = str_replace('\\', '/', 'C:\Windows\Fonts\simsun.ttc');</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'C:\Windows\Fonts\simhei.ttf'<span style="color: #000000;">);    </span><span style="color: #800080;">$text</span> = '世界如此美妙,我却如此暴躁'<span style="color: #000000;">;      </span><span style="color: #008000;">//</span><span style="color: #008000;"> 需要转换编码吗?    //$text = iconv('ANSI', 'UTF-8', $text);     // 画文字,text需要时UTF-8编码</span>    imagettftext(<span style="color: #800080;">$image</span>, 12, 0, 20, 50, <span style="color: #800080;">$blue</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出图像</span>    <span style="color: #008080;">header</span>('Content-Type:image/png'<span style="color: #000000;">);    imagepng(</span><span style="color: #800080;">$image</span><span style="color: #000000;">);    imagedestroy(</span><span style="color: #800080;">$image</span>);

  效果: 

  在使用字体时,偶发现simsun.ttc这个是简体中文,扩展是.ttc,但是用imagettftext也能用。另,如果你的编辑器编辑的代码不是UTF-8编码,需要使用iconv将文本编码转换过来,特别是文本变量是从数据库中读取时,但是你懂的,什么ANSI、GBK、GB2312等转换为UTF-8时,不出意外会是乱码-_-#。既然能用字体了,就到网上扒了几个免费字体玩了下(比如这个http://www.freefontscenter.com/),这个测试程序前提是需要下载对应字体文件,放到指定目录,注意有的字体扩展是.otf,这个OpenType字体,用imagettftext就不行啦,要用imagefttext ~

<span style="color: #000000;">php        </span><span style="color: #800080;">$width</span> = 600; <span style="color: #800080;">$heiht</span> = 150<span style="color: #000000;">;    </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$heiht</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加颜色</span>    <span style="color: #800080;">$blue</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0x40, 0x99, 0xff); <span style="color: #008000;">#</span><span style="color: #008000;">229cdc</span>    <span style="color: #800080;">$green</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0x59, 0x98, 0x3b<span style="color: #000000;">);    </span><span style="color: #800080;">$yellow</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 0xff, 0xe6, 0xa4<span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 填充背景</span>    imagefilledrectangle(<span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$heiht</span>, <span style="color: #800080;">$yellow</span><span style="color: #000000;">);    </span><span style="color: #800080;">$text</span> = 'lazycat'<span style="color: #000000;">;    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 1</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\Maiandra_GD.ttf'<span style="color: #000000;">);     imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 5, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 2</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\black_jack.ttf'<span style="color: #000000;">);      imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 85, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);      </span><span style="color: #008000;">//</span><span style="color: #008000;"> 3</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\OpenSans-Italic.ttf'); <span style="color: #008000;">//</span>     imagefttext(<span style="color: #800080;">$image</span>, 17, 0, 155, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 4</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\Amatic-Bold.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 235, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 5</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\Santana-RegularCondensed.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 290, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 6</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\veteran_typewriter.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 345, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 7</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\LondrinaOutline-Regular.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 20, 0, 440, 50, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 8</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\SCRATCHM.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 5, 100, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 9</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\tiza.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 150, 100, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 10</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\texgyrecursor-regular.otf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 325, 100, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 11</span>    <span style="color: #800080;">$fontfile</span> = <span style="color: #008080;">str_replace</span>('\\', '/', 'G:\wamp\fonts\free\yukarimobil.ttf'<span style="color: #000000;">);    imagefttext(</span><span style="color: #800080;">$image</span>, 17, 0, 430, 100, <span style="color: #800080;">$green</span>, <span style="color: #800080;">$fontfile</span>, <span style="color: #800080;">$text</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出图像</span>    <span style="color: #008080;">header</span>('Content-Type:image/png'<span style="color: #000000;">);    imagepng(</span><span style="color: #800080;">$image</span><span style="color: #000000;">);    imagedestroy(</span><span style="color: #800080;">$image</span>);

  效果:

      

  非常炫酷有木有!>3

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn