Home >php教程 >PHP源码 >封装了一个类,可生成验证码,缩略图,及水印图

封装了一个类,可生成验证码,缩略图,及水印图

PHP中文网
PHP中文网Original
2016-05-26 08:18:121034browse

php代码

<?php
class image{
	session_start();
    //验证码类
    static public function verify($code,$width=75,$height=25,$n=4){
        header("content-type:image/png");
        // 创建画布
        $img=imagecreatetruecolor($width,$height);
        // 设置背景色
        $bgcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));
        // 将背景色填充
        imagefill($img,0,0,$bgcolor);
        // 绘制五条弧线
        for($i=0;$i<5;$i++){
            $arccolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));
            imagearc($img,mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(5,($width-5)),mt_rand(5,($height-5)),mt_rand(0,360),mt_rand(0,360),$arccolor);
        }
        // 绘制一百个点
        for($i=0;$i<100;$i++){
            $pixelcolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));
            imagesetpixel($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),$pixelcolor);
        }
        // 绘制五条线段
        for($i=0;$i<5;$i++){
            $linecolor=imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),rand(200,255));
            imageline($img,mt_rand(1,($width-1)),mt_rand(1,($height-1)),mt_rand(1,($width-1)),mt_rand(1,($height-1)),$linecolor);
        }
        // 设置边框颜色
        $bdcolor=imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),rand(150,200));
        // 绘制一个矩形无填充边框
        imagerectangle($img,0,0,($width-1),($height-1),$bdcolor);
        // 设置验证码字符串
        $str=&#39;&#39;;
        for($i=1;$i<=$n;$i++){
            $str.=substr(str_shuffle($code),0,1);
        }
        $_SESSION[&#39;a&#39;]=$str;
        //
        for($i=0;$i

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn